This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

how to release memory after malloc()?



I use the DM365 to compress jpeg pics, and send them out through UDP socket, I use sendto() to send out the content, but after few minutes, it stopped working, and print messages below:


oom-killer: gfp_mask=0x200d2, order=0
Mem-info:
DMA per-cpu:
cpu 0 hot: high 18, batch 3 used:2
cpu 0 cold: high 6, batch 1 used:5
DMA32 per-cpu: empty
Normal per-cpu: empty
HighMem per-cpu: empty
Free pages:        1028kB (0kB HighMem)
Active:6776 inactive:1174 dirty:0 writeback:0 unstable:0 free:257 slab:584 mapped:134 pagetables:146
DMA free:1028kB min:884kB low:1104kB high:1324kB active:27104kB inactive:4696kB present:49152kB pages_scanned:3020 all_unreclaimable? no
lowmem_reserve[]: 0 0 0 0
DMA32 free:0kB min:0kB low:0kB high:0kB active:0kB inactive:0kB present:0kB pages_scanned:0 all_unreclaimable? no
lowmem_reserve[]: 0 0 0 0
Normal free:0kB min:0kB low:0kB high:0kB active:0kB inactive:0kB present:0kB pages_scanned:0 all_unreclaimable? no
lowmem_reserve[]: 0 0 0 0
HighMem free:0kB min:128kB low:128kB high:128kB active:0kB inactive:0kB present:0kB pages_scanned:0 all_unreclaimable? no
lowmem_reserve[]: 0 0 0 0
DMA: 39*4kB 1*8kB 0*16kB 1*32kB 1*64kB 0*128kB 1*256kB 1*512kB 0*1024kB 0*2048kB 0*4096kB 0*8192kB 0*16384kB = 1028kB
DMA32: empty
Normal: empty
HighMem: empty
Swap cache: add 0, delete 0, find 0/0, race 0+0
oom-killer: gfp_mask=0xd0, order=0
Free swap  = 0kB
Total swap = 0kB
Mem-info:
DMA per-cpu:
cpu 0 hot: high 18, batch 3 used:2
Free swap:            0kB
cpu 0 cold: high 6, batch 1 used:5
12288 pages of RAM
369 free pages
2666 reserved pages
584 slab pages
DMA32 per-cpu:473 pages shared
0 pages swap cached
Out of Memory: Kill process 517 (av_server.out) score 3042 and children.
Out of memory: Killed process 517 (av_server.out).
 empty
Normal per-cpu: empty
HighMem per-cpu: empty
Free pages:        1032kB (0kB HighMem)
Active:6139 inactive:1811 dirty:0 writeback:0 unstable:0 free:258 slab:585 mapped:134 pagetables:146
DMA free:1032kB min:884kB low:1104kB high:1324kB active:24556kB inactive:7244kB present:49152kB pages_scanned:4827 all_unreclaimable? no
lowmem_reserve[]: 0 0 0 0
DMA32 free:0kB min:0kB low:0kB high:0kB active:0kB inactive:0kB present:0kB pages_scanned:0 all_unreclaimable? no
lowmem_reserve[]: 0 0 0 0
Normal free:0kB min:0kB low:0kB high:0kB active:0kB inactive:0kB present:0kB pages_scanned:0 all_unreclaimable? no
lowmem_reserve[]: 0 0 0 0
HighMem free:0kB min:128kB low:128kB high:128kB active:0kB inactive:0kB present:0kB pages_scanned:0 all_unreclaimable? no
lowmem_reserve[]: 0 0 0 0
DMA: 39*4kB 14*8kB 0*16kB 2*32kB 1*64kB 0*128kB 1*256kB 1*512kB 0*1024kB 0*2048kB 0*4096kB 0*8192kB 0*16384kB = 1164kB
DMA32: empty
Normal: empty
HighMem: empty
Swap cache: add 0, delete 0, find 0/0, race 0+0
Free swap  = 0kB
Total swap = 0kB
Free swap:            0kB
12288 pages of RAM
379 free pages
2666 reserved pages
585 slab pages
478 pages shared
0 pages swap cached
Out of Memory: Kill process 531 (av_server.out) score 3042 and children.
Out of memory: Killed process 531 (av_server.out).

It seems the memory was not released, but I have addded free() after every malloc(), what else I need to do? thanks.

  • Clark,

    The log indicates the OOM Killer was invoked. It is an extreme step that is performed when all other mechanisms to free memory have failed.

    The processes that get killed are the ones which have highest "badness" scores. Based on the log, av_server.out seems be leaking memory.

    Clark Wang said:
    It seems the memory was not released, but I have addded free() after every malloc(), what else I need to do?

    You are right that memory allocated via malloc() can be freed via free(). However, you would need to match the calls to allocate and free the memory. Specific conditions to look for would be:

    • Unbalanced calls to malloc() and free() in loops. Look for exit conditions in the loops.
    • Calls to free() that could be missed due to error conditions.
    • Algorithm/ library you are using - could be leaking the memory as well - though your glue logic may be balanced on its malloc() and free() calls.

    I wouls also suggest going through this file in the kernel sources to help you identify the source of leaks - Documentation/kmemleak.txt

  • Hi,

    Can you confirm if the same OOM Killer comes up when you are not streaming the packets? I mean you keep on doing JPEG encode and discard the encoded output. I am wondering if it is coming from the networking stack.

    Regards,

    Anshuman