Find the answer to your Linux question:
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 ...
  1. #1
    Just 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.

  2. #2
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    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?

  3. #3
    scm
    scm is offline
    Linux 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.

  4. #4
    Linux User Dark_Stang's Avatar
    Join Date
    Jun 2006
    Location
    Around St. Louis
    Posts
    284
    In C++ you have to do your own garbage collection to prevent memory leaks.
    Two levels higher than a newb.
    (I can search google)

  5. #5
    Just 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.

  6. #6
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by dineshsethiya View Post
    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.
    Yes, that's exactly what I mean. I may be wrong about the process not decreasing in size, though, and would be grateful for confirmation from anyone more knowledgeable on the Linux side of things than I am.

  7. #7
    Linux User
    Join Date
    Jul 2004
    Location
    Poland
    Posts
    368
    Quote Originally Posted by scm View Post
    Yes, that's exactly what I mean. I may be wrong about the process not decreasing in size, though, and would be grateful for confirmation from anyone more knowledgeable on the Linux side of things than I am.
    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:

    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;
    }
    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:
    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"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...