Results 1 to 7 of 7
Hi,
When I'm running my C++ binary, and see the memory usage and %cpu using top command, I can see that Resident memory is keep increasing as I increased the ...
- 09-13-2007 #1Just Joined!
- Join Date
- Sep 2007
- Posts
- 5
Resident Memory keep growing
Hi,
When I'm running my C++ binary, and see the memory usage and %cpu using top command, I can see that Resident memory is keep increasing as I increased the load ( inputs ) on my binary. If I stop the input/load then also it Resident memory does not decrease. What are the implications of this ? Does anyone has got any idea.
Thanks in advance.
- 09-13-2007 #2
Well, Sounds pretty normal unless you mean that the resident memory never goes down afterwards. In that case, it sounds like you've got a memory leak. I'm not too experienced in c++, i mostly have only done C, and FWIU it's a little harder to get a memory leak in C++. Are you implementing any stacks or linked lists with your own code rather than the c++ classes for them?
- 09-13-2007 #3Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
Isn't a process's memory done like a ratchet? It'll grow as it needs to but will never shrink (like a filesystem directory), there'll just be available memory when it needs to allocate and won't have to grow the process every time. Of course, that doesn't rule out the possibility that the process leaks.
- 09-13-2007 #4
In C++ you have to do your own garbage collection to prevent memory leaks.
Two levels higher than a newb.
(I can search google)
- 09-14-2007 #5Just Joined!
- Join Date
- Sep 2007
- Posts
- 5
Thanks guys for your replies. scm do u mean that Resident memory will keep increase and at one point of time it will stop increasing when enough memory is available to serve the peak load. Also do u mean that this max memory will never decrease even if we reduce the load/input to the process.
Many thanks in advance.
- 09-14-2007 #6Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
- 09-14-2007 #7Linux User
- Join Date
- Jul 2004
- Location
- Poland
- Posts
- 368
This depends on malloc() implementation. The malloc from the GNU libc library is known to return the memory to the system if it is no longer necessary (this feature is tunable). You can observe this behavior by running the following program together with ps or top:
It does so in units called pages (4KB chunks on x86 machines). If a page contains even a smallest allocated memory fragment it won't be reclaimed. This might be the case of C++ allocator, which:Code:#include <stdio.h> #include <stdlib.h> #include <string.h> enum { KB = 1024, MB = 1024 * KB }; int main() { void* mem; puts("allocating... check mem usage and hit enter"); mem = malloc(100 * MB); getchar(); puts("zeroing... check mem usage and hit enter"); memset(mem, 0, 100 * MB); getchar(); puts("freeing... check mem usage and hit enter"); free(mem); getchar(); return EXIT_SUCCESS; }
1) might not use malloc to get memory from system
2) might maintain its own bookkeeping information which clobbers the pages
3) other reasons?
Hope this helps,
Kind regards"I don't know what I'm running from
And I don't know where I'm running to
There's something deep and strange inside of me I see"


Reply With Quote
