LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Application Virtual address space memory allocation - memory does not get free (http://www.linuxquestions.org/questions/linux-general-1/application-virtual-address-space-memory-allocation-memory-does-not-get-free-803199/)

chamara82 04-20-2010 11:49 PM

Application Virtual address space memory allocation - memory does not get free
 
I am using malloc and frees a lot in my program. It shows its allocated but when i remove it doesnt show as the memory is removed(I am using the top command to view VIRT memory usage). If this continously grows what would happen to my program (Will it go out of memory?)

paulsm4 04-21-2010 12:36 AM

You only need to be alarmed if you keep seeing memory usage GROW even after you know you've freed it.

It would be extremely inefficient to shrink and grow program size every time you malloc and free new data. As an optimization, "free()" only marks de-allocated memory as "available", and "malloc()" tries to re-use previously allocated memory whenever possible.

So no, there's probably nothing to worry about - the system is doing exactly what it's supposed to :)

chamara82 04-21-2010 02:49 AM

I have checked my program for memory leaks and found None(Valgrind,purify). I have growing memory problem each time i execute some tasks of the program memory grows but the growth degrades after number of times
E.g.
times i execute the same tasks
1st time VIRT memory - 1023MB
2nd time - 1123MB
3rd Time - 1190MB
4th Time - 1210MB
5th Time - 1220MB
So the memeory growth degrades as we go along. If there is no problem with memory leaks, can we make sure that the freed memory will be available for the process regardless of this growth shown. THere is a virtual addess space cutover at 3GB in 32bit RH4 i guess.

Does anyone knw what will happen if we malloc and free small chunks of memory(3KB) many times. Does the virtual address space do a memory fragementaion? Please help

johnsfine 04-22-2010 06:53 AM

Did you start two threads for this same question? Or did you change your user ID and start three threads for the one question:
http://www.linuxquestions.org/questi...-leaks-803024/

http://www.linuxquestions.org/questi...-space-803426/

Having part of the details we need to give a specific answer in each of three threads makes it much more difficult to provide an answer.

Quote:

Originally Posted by chamara82 (Post 3942311)
I have checked my program for memory leaks and found None(Valgrind,purify).

That isn't a strong reason to believe there are no memory leaks. Valgrind and purify are not very accurate. There are many kinds of leaks they can't report.

Quote:

times i execute the same tasks
That phrase needs a lot more clarification. In context of the thread, it would seem you mean the program is running continuously and the program does some operation which malloc's a bunch of memory and the frees all of it before the next time. If that were true, you would not see the growth in VIRT that you claim. So probably you do have a memory leak.

But that estimate is extrapolating a lot from your unclear statements.

Quote:

Originally Posted by chamara82 (Post 3943457)
But the growth is degrading over the number of times of the execution of the tasks. Can i think that fragmentation plays a role in this?

Consider a program that has a lot of small allocations and never frees a large fraction of them at once. Instead it might replace them one at a time with similar, but not exactly the same size, allocations.

When malloc allocates a chunk, it might not have a free one of exactly the right size nor have a big contiguous free area, so it might split a slightly larger chunk into the chunk needed and a small fragment that won't get used. If you later need the size that it split, it might no longer be available, causing malloc to split a larger chunk again. Soon malloc would need to get more from the OS even though the total of all allocations might not be changing much.

So in that rolling replace mode (unlike the deleting it all at the end of each task and reallocating for the next task) the average amount wasted for allocations might gradually go up as the rolling replace varies the chunk sizes.

But the fragments aren't lost forever. They come back when you free the adjacent chunks. So as you continue such a rolling replace, the amount that the average freed size exceeds the actual use should soon catch up with the amount the average allocation cost exceeds the actual use.

All of that assumes a rolling replace design with random small size variations. That is an even wilder extrapolation from the little info you have provided.

chamara82 01-01-2011 08:19 PM

Sorry for the delay in replying this thread. Do you know a tool to check actual memory usage for a process. Why i am asking this is I have a process it actually allocates small chuncks of data and release everything at the end. However the top command does not seem to show that. I used purify and valigrind to check for memory leaks too but didnt find any leaks on my program. I am currently looking at VIRT from the top, it actually shows an increase all the time. How am i suppose to know that the freed memory is aavailable to process and they can be reused since the there is not +/- shown in the VIRT column in top,


All times are GMT -5. The time now is 09:45 PM.