trying to identify a memory leak in my program. please help me here.

This is a discussion on trying to identify a memory leak in my program. please help me here. within the Linux Programming forums, part of the Platform Specific Boards category; hello all, Iam trying to identify a memory leak in my program , iam using ps to discover/get confirmation that ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    108

    trying to identify a memory leak in my program. please help me here.

    hello all,

    Iam trying to identify a memory leak in my program , iam using ps to discover/get confirmation that i do have memory issue in my program . so after regular interval i am issuing ps command and i see that vmsize of my task is getting increased is this correct way of justifying the memory leak issue?


    let me give you a design overview of my process.

    It has two threads . one thread is a producer of data another thread is consumer of that data.

    This data is a list , so thread 1 would first generate the list and then intimate the thread 2 that new list available .

    thread 2 would be then using that copy data/ list to local list . this list consist of domain name which needs to be get resolve . thread 2 would resolve the domian name and it compares local list any change with the exisiting global list .

    if there is a change it will free the global list and then make local list as global.

    Here memory leak is happening only when it is free the existing global list by thread2 .

    But the same free routine works for thread1,where i donot see any memory leak .
    I could not able to figure out why this is failing when this free routine is getting used by thread2

    your help would be highly appreciated.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,307
    Quote Originally Posted by vlrk View Post
    so after regular interval i am issuing ps command and i see that vmsize of my task is getting increased is this correct way of justifying the memory leak issue?
    Not with certainty, no. All ps will tell you is whether the process memory usage is increasing, decreasing, or remaining static. Even if memory usage decreases between two measurements, there may still be a memory leak .... if some amount of memory released exceeds what is leaked.

    If memory usage remains static or increases between two measurements, there may or may not be a leak. Not all cases where memory usage increases (or does not decrease) are attributable to memory leaks.

    A continually increasing memory usage MIGHT be a symptom of a memory leak. But it might also mean that the program is behaving as intended, is allocating memory to do some long-duration task but - since it has not completed that task - may not yet have released that memory.

    What you really need to do is analyse the design of your program, and the code, to account for memory usage.

    If there is any potential for memory to be allocated, but never released, then you may have a leak. If you are able to establish that all memory allocated will be deallocated exactly once (deallocating something more than once is actually a nastier problem than a memory leak) then you can argue convincingly that there is NOT a memory leak.

    You might also use debugging tools (like valgrind) to gather data about various memory problems, including potential leaks. This is the most common approach, practically, because most people are not good enough at analysing software design nor at analysing large amounts of source code in order to determine if their program is behaving as intended.

    However, tools like valgrind can give false positives (identify problems that don't exist) and false negatives (missing problems that do exist), because they only gather data about code that is executed. If some critical code is not executed, then valgrind will not gather information about what it is doing (whether good or bad).
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    108
    I also use free and top which also shows me continuous decrease of memory which is free.

    what is vmsize indicates in ps ?

    i cannot run valgrind on that . that is one of remote target board .
    which does not have valgrind compiled for that environment.

    so any other ways or tips to narrow down this issue

    thanks

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Using LD, the GNU linker - Options
    --wrap symbol
    Use a wrapper function for symbol. Any undefined reference to symbol will be resolved to __wrap_symbol. Any undefined reference to __real_symbol will be resolved to symbol. This can be used to provide a wrapper for a system function. The wrapper function should be called __wrap_symbol. If it wishes to call the system function, it should call __real_symbol. Here is a trivial example:
    Code:
        void *
        __wrap_malloc (int c)
        {
          numMallocCalls++;
          return __real_malloc (c);
        }
    Periodically print the counters to see if your numMallocCalls is getting away from your numFreeCalls

    Don't forget to include calloc in this, and also do the right thing in realloc.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    108
    salem,

    thanks for the reply,

    i am struggling to use this wrapper in to my program.

    Let me show a simple example which i am trying here.

    I do have one code test.c as follows

    Code:
     
    #include <stdio.h>
    
    int main()
    {
    	char *test;
    	
    	test = __wrap_malloc(10);
    	
    	sprintf(test,"testing");
    	printf("%s",test);
    	free(test);
    
    	return 0;
    }
    and another testm.c as follows

    Code:
     
    #include <stdlib.h>
    
    int numMallocCalls;
    
    void *
    __wrap_malloc(int c)
    {
         numMallocCalls++;
         return __real_malloc(c);
    }
    trying to link them with the following command

    Code:
     
    ld -o test testm.o test.o --wrap malloc

    it gives me following error

    Code:
     
    ld: warning: cannot find entry symbol _start; defaulting to 0000000008048094
    testm.o: In function `__wrap_malloc':
    testm.c:(.text+0x1a): undefined reference to `malloc'
    test.o: In function `main':
    test.c:(.text+0x32): undefined reference to `memcpy'
    test.c:(.text+0x47): undefined reference to `printf'
    test.c:(.text+0x53): undefined reference to `free'

    can you please let me know where am i going wrong here ..?

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Invoke the linker through the compiler, since it will pass all the correct standard library options as well.

    gcc -o test testm.o test.o --wrap malloc
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    108
    $ gcc -o test testm.o test.o --wrap malloc
    gcc: malloc: No such file or directory


    any thing i am missing here..?

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Sorry, forgot it was a linker-specific option.
    Code:
    $ cat foo.c
    #include <stdio.h>
    #include <stdlib.h>
    extern void *__wrap_malloc(size_t);
    
    // for testing
    extern int numMallocCalls;
    
    int main()
    {
        char *test;
    
        test = __wrap_malloc(10);
    
        sprintf(test,"testing");
        printf("%s\n",test);
        free(test);
        printf("Stats=%d\n",numMallocCalls);
        return 0;
    }
    $ cat bar.c
    #include <stdlib.h>
    extern void *__real_malloc(size_t);
    int numMallocCalls;
    
    void *
    __wrap_malloc(int c)
    {
         numMallocCalls++;
         return __real_malloc(c);
    }
    $ gcc foo.c bar.c -Wl,--wrap=malloc
    $ ./a.out 
    testing
    Stats=1
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    108
    Thx salem .

    It did work for me with this.

    It does show me correct malloc and free calls with wrapper.

    still i see with ps "vmsize" for the process is getting increased continoulsy, i feel some thing is missing at my end .

    I need to look it more carefully

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Vmsize does not reflect the actual amount of ram used. Read this:

    VIRTUAL ADDRESS SPACE VS. PHYSICAL MEMORY

    That's from a man page for a tool I wrote last year, but the logic also applies to the info from top or ps (since all these things get their data from the same place). The tool itself may or may not be useful to you, it has some fields not available in other monitors but it works on a much coarser timescale:

    Plog

    Top or htop are much much better for monitoring a process in real time than ps is, since ps is not persistent.

    htop - an interactive process-viewer for Linux

    Keep in mind that address space is not necessarily released immediately when free() is called. You might try throwing this in after your free calls, which can make a noticeable difference (altho in "the big picture" is not so significant):

    Code:
    #include <malloc.h>
    
    malloc_trim(0);
    Nb. that is linux specific.
    Last edited by MK27; 01-25-2012 at 10:18 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory leak checker program? for windows
    By rahulsk1947 in forum C Programming
    Replies: 1
    Last Post: 11-12-2007, 06:20 AM
  2. How to identify that user has terminated the program
    By abhijitbanerjee in forum C Programming
    Replies: 2
    Last Post: 07-11-2007, 04:43 PM
  3. Replies: 2
    Last Post: 09-28-2006, 01:06 PM
  4. Replies: 6
    Last Post: 06-27-2006, 08:35 AM
  5. Replies: 5
    Last Post: 01-18-2006, 10:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21