Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm running a C++ program in Ubuntu 10.04 (32-bit system architecture). If I calculate the amount of memory that my program uses, it comes up to 800MB. I have a 4GB RAM in place. But still before the program even finishes it throws an out of memory exception. Why is that happening ? Is it because of the structure of the memory or implementation problems or what could possibly trigger this issue ? I've had seen this problem quite a number of times before but never understood the reason behind it. Have any of you handled this case before ?

Update:

The program I'm trying to run is here with explanation. To increase the amount of memory I use I just icrease the value of max_i in the main function.

share|improve this question
    
What's your system architecture - 32bit or 64bit? If you have 32-bit, then you're limited to 2GB per process memory limit despite the amount of total RAM installed. Also, if you have 64bit architecture, but 32bit executable, you hit the same limitation. There are other limitations, such as per-user memory limits set by ulimit. –  mbaitoff Feb 28 '11 at 5:00
    
@mbaitoff: It's 32 bit. –  Sunil Feb 28 '11 at 5:06
2  
Then you're up to 2GB. But please understand, that this is the summary maximum amount of memory theoretically available to your application. That doesn't imply you'd be able to allocate 1GB of space at once - previous allocations and deallocations may have fragmented the heap, so you may encounter the situation when your allocation request fails even when available amount of memory is more than requested. Also, you should have a debug info about an allocation attempt that caused the out-of-memory situation - check it. –  mbaitoff Feb 28 '11 at 5:12
    
In my program I didn't de allocate or call destructor for anything mainly because I'm mostly concerned how the memory allocation works and I want to check how much memory my program can consume. Also I read some where that the memory is allocated in a heap structure. Now that you mentioned heap. Can you elaborate on that? –  Sunil Feb 28 '11 at 5:17
1  
I think nothing could be done until we see that magic part of a program that tries to allocate and fails. –  mbaitoff Feb 28 '11 at 6:16
show 2 more comments

2 Answers

We need more information. It seems like you're doing an experiment which is cool, but what does the program actually do.

Are you allocating thousands of small chunks of memory or a few large continuous chunks etc...

The question is bordering on stackoverflow suitability IMO.

As the other guys suggest you are in a 32bit environment and your process could well be struggling to allocate a large chunk in one hit. But, I'm just speculating without more details.

share|improve this answer
    
I'm trying to allocate large amount of small chunks. Say for example, in one of my experiments, I defined a class and created large number of instances of the class using a nested for loop. Basically I'm trying to analyze how much memory can be taken by a process of there are large amount of small chunks vs small amount of large chunks. I don't understand the allocation. –  Sunil Mar 1 '11 at 14:51
add comment

Things that could go wrong:

  • The system is not able to give you X bytes of contineous memory. Lets say you want a block of 512MB, but the system is not able to find such a large, contineous block. Then you are out of luck and will get a "out of memory" exception

  • You miscalculate the amount of memory your programm actually needs. Do you free up unused memory? Did you calculate the overhead for some structures (keyword alignment) correctly?

  • Do you have a big enough swap-file so the system can swap out other processes?

share|improve this answer
    
I did not de allocate anything and I'm pretty sure I calculated the memory that will be used properly. Can you explain a little more about swap ? I'm interested to know about how swapping memory works in general. –  Sunil Feb 28 '11 at 5:20
    
en.wikipedia.org/wiki/Paging –  mbaitoff Feb 28 '11 at 6:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.