Search code examples
debuggingmemory-leakssolarislibumem

Solaris libumem why not show memory leak for first dynamic allocation


Say

void main()
{
    void *buff;
    buff = malloc(128);
    buff = malloc(60);
    buff = malloc(30);
    buff = malloc(16);
    free(buff);
    sleep(180);
}

ulib mem in solaris10 show only 60 bytes and 30 bytes as leak , why it not show 128 bytes also leaked?


Solution

  • Three memory leaks are detected by mdb and libumem with your code here:

    cat > leak.c <<%
    int main() { void *buff; buff = malloc(128); buff = malloc(60); buff = malloc(30); buff = malloc(16); free(buff); sleep(180); }
    %
    gcc -g leak.c -o leak
    pkill leak
    UMEM_DEBUG=default UMEM_LOGGING=transaction LD_PRELOAD=libumem.so.1 leak &
    sleep 5
    rm -f core.leak.*
    gcore -o core.leak $(pgrep leak) 
    mdb leak core.leak.* <<%
    ::findleaks -d
    %
    gcore: core.leak.1815 dumped
    CACHE     LEAKED   BUFCTL CALLER
    0807f010       1 0808b4b8 main+0x29
    0807d810       1 0808d088 main+0x39
    0807d010       1 08092cd0 main+0x49
    ------------------------------------------------------------------------
       Total       3 buffers, 280 bytes
    
    umem_alloc_160 leak: 1 buffer, 160 bytes
                ADDR          BUFADDR        TIMESTAMP           THREAD
                                CACHE          LASTLOG         CONTENTS
             808b4b8          8088f28      1af921ab662                1
                              807f010          806c000                0
                     libumem.so.1`umem_cache_alloc_debug+0x144
                     libumem.so.1`umem_cache_alloc+0x19a
                     libumem.so.1`umem_alloc+0xcd
                     libumem.so.1`malloc+0x2a
                     main+0x29
                     _start+0x83
    
    umem_alloc_80 leak: 1 buffer, 80 bytes
                ADDR          BUFADDR        TIMESTAMP           THREAD
                                CACHE          LASTLOG         CONTENTS
             808d088          808cf68      1af921c11eb                1
                              807d810          806c064                0
                     libumem.so.1`umem_cache_alloc_debug+0x144
                     libumem.so.1`umem_cache_alloc+0x19a
                     libumem.so.1`umem_alloc+0xcd
                     libumem.so.1`malloc+0x2a
                     main+0x39
                     _start+0x83
    
    umem_alloc_40 leak: 1 buffer, 40 bytes
                ADDR          BUFADDR        TIMESTAMP           THREAD
                                CACHE          LASTLOG         CONTENTS
             8092cd0          808efc8      1af921f2bd2                1
                              807d010          806c0c8                0
                     libumem.so.1`umem_cache_alloc_debug+0x144
                     libumem.so.1`umem_cache_alloc+0x19a
                     libumem.so.1`umem_alloc+0xcd
                     libumem.so.1`malloc+0x2a
                     main+0x49
                     _start+0x83