Click here to Skip to main content
15,919,479 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,
All I was trying to do was to write a C program implementing linked list which does the following operations.

1. Inserting numbers from 1 to 100000000 by iterating in a loop.
2. Freeing the consumed memory by the program.

The program runs successfully for smaller limits but fails for the above.

I see that my process is being killed by OS after a while.

I issued dmesg command on the prompt and I see the following message
"Out of memory: Kill process 6881 ( a.out)".

I understand that on a 32 bit platform a process can't consume more than 4GB of memory. Is this totally true?

But how do I know if my process consumes more than 4GB of memory even before executing it????

For my problem I want to implement a linked list because I do not know how much data I will get as an input.

compiler: gcc 4.4.5
OS: fedora

Need your help :)
Posted

32 bits of addressing is 2^32 or 4GB.

In a practical sense, 32 bit applications are commonly limited to 2^31 or 2 GB.
There is a linker switch in Visual Studio to change this limit to 4gb.

To figure out how much memory your process will consume, you'll have to do some math.

100000000 integers is 400 megabytes.

Add two 32 bit pointers, now you're up to 1.2 gigabytes.

But wait, there's more...

Memory is typically allocated in 16 byte blocks so each record will consume at least 16 bytes. At least 4 bytes of that memory is housekeeping. But with two pointers and an integer we're right at 16 bytes.

So now we're up to 1.6 gigabytes.

If you're running in debug mode, a lot more data goes into the allocation and your minimum record size can be much larger. So you might be bumping up against 8, 16, or more gigabytes.
 
Share this answer
 
Comments
[no name] 1-Nov-12 14:29pm    
Thank you and I agree the way the program's memory is calculated but how about fixing the problems related to these types???
JackDingler 1-Nov-12 14:32pm    
Change your algorithm.

There is nothing wrong with the data types. Your code is not making efficient use of memory.

See this article for an example of a creative way to work with large numbers of integers with very little memory usage.

http://preshing.com/20121026/1mb-sorting-explained
JackDingler 2-Nov-12 10:08am    
If you really, really need to do this processing in memory... Perhaps for real time simulations?

Build in 64 bit mode and run on a 64 bit system with at least 16gigabytes of RAM.

Also you may consider changing you memory structures from a linked list, to to fixed records that have many elements on each record. This will reduce memory usage per element as you won't need one or two pointers for each integer.
Nelek 1-Nov-12 14:34pm    
+5
take the variables as unsigned long.

like:
unsigned long x=4294967295( maximum value)
It can only store integers.
 
Share this answer
 
Comments
[no name] 1-Nov-12 14:15pm    
My problem is not with the datatype but why the OS has killed my process. The kernel should not be bothered about the datatype that I am using in my program!!!!!
Something out of the box bothers the kernel. That is why it killed my process.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900