且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

调用free()后未释放内存

更新时间:2023-02-02 21:36:09

如果通过该进程继续保留分配的内存",则表示ps没有报告该进程的内存使用量减少,这是完全正常的.由于种种原因,将内存返回到进程的堆并不一定会使该进程将其返回到操作系统.如果您在一个大循环中一次又一次地创建和销毁列表,并且进程的内存使用没有无限增长,那么您可能没有真正的内存泄漏.

If by "the process continues to hold the allocated memory" you mean that ps doesn't report a decrease in the process's memory usage, that's perfectly normal. Returning memory to your process's heap doesn't necessarily make the process return it to the operating system, for all sorts of reasons. If you create and destroy your list over and over again, in a big loop, and the memory usage of your process doesn't grow without limit, then you probably haven't got a real memory leak.

[再次编辑以添加:顺便说一下,分配512字节块使问题消失的最可能原因是,您的malloc实现以某种方式特别对待较大的块,使它在出现时更容易注意到是不再使用的整个页面-如果要将所有内存返回给操作系统,这是必需的.]