强制free()将malloc内存返回给OS [英] Force free() to return malloc memory back to OS

查看:123
本文介绍了强制free()将malloc内存返回给OS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎就像我释放了由malloc()分配的Linux进程的所有内存之后, 内存仍为该进程保留,没有返回给操作系统.

Seems like even after I free all the memory for a Linux process that was allocated by malloc(), memory is still reserved for the process and not returned to the OS.

默认情况下,运行valgrind massif工具不会泄漏.

Running valgrind massif tool by default revelas no leakges.

使用--pages-as-heap = yes运行valgrind会显示以下内容:

Running valgrind with --pages-as-heap=yes reveals this:

-> 13.77%(7,655,424B)0x35FEEEB069:brk(brk.c:31)

->13.77% (7,655,424B) 0x35FEEEB069: brk (brk.c:31)

-> 13.77%(7,655,424B)0x35FEEEB113:sbrk(sbrk.c:53)

->13.77% (7,655,424B) 0x35FEEEB113: sbrk (sbrk.c:53)

-> 13.77%(7,655,424B)0x35FEE82717:__default_morecore(morecore.c:48)

->13.77% (7,655,424B) 0x35FEE82717: __default_morecore (morecore.c:48)

-> 13.77%(7,655,424B)0x35FEE7DCCB:_int_malloc(malloc.c:2455)

->13.77% (7,655,424B) 0x35FEE7DCCB: _int_malloc (malloc.c:2455)

-> 13.77%(7,655,424B)0x35FEE7F4F1:malloc(malloc.c:2862)

->13.77% (7,655,424B) 0x35FEE7F4F1: malloc (malloc.c:2862)

因此,即使内存已经由free()释放了,看起来malloc仍然调用了brk/sbrk,并且没有将其返回给操作系统.

so even though memory was already freed by free(), it seems that malloc called brk/sbrk and did not return this to the OS.

如何强制free()立即调用sbrk()并将所有内存返回给OS?

how can I force free() to call sbrk() immedeaitly and return all memory back to the OS ?

我正在一个非常低端的平台上运行,每个MB都很重要.

I am running on a very low end platform which every MB counts.

谢谢.

推荐答案

拥有操作系统回收内存的唯一可靠且可移植的方法是退出该进程,然后再次重新启动它,以恢复您的任何状态需要继续.

The only reliable and portable way to have the OS reclaim memory is to exit the process and restart it again, restoring any state you need to continue.

当然,另一个选择是根据需要使用brk/sbrk编写自己的malloc/free实现.

Of course, writing your own malloc/free implementation using brk/sbrk according to your needs is the other option.

这篇关于强制free()将malloc内存返回给OS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆