gc.collect真的释放内存吗?

发布于 2025-01-14 21:01:04 字数 323 浏览 5 评论 0原文

我正在尝试使用 gc.collect() 释放内存。但我不太明白它是如何工作的。例如:

在此处输入图像描述

在运行 del 和 gc 之前,使用的内存为 58g。运行后,使用的内存仍然是58。看起来没有释放任何内存。我感觉 gc 与关闭 python 并重新启动不同,后者实际上释放了内存。

所以我想知道如何实际释放内存以便我可以加载其他数据?

I am trying to release memory using gc.collect(). But I do not really understand how it works. For example:

enter image description here

Before I run del and gc, the used memory is 58g. After running them, the used memory is still 58. It looks that no memory has been released. I sense gc is different than shutting down python and starting again, which actually release memory.

So I am wondering how to actually release the memory so that I can load other data?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风铃鹿 2025-01-21 21:01:04

根据您的操作系统,它可能不会收回释放的内存。

您可以在 Linux 上尝试 malloc_trim

import ctypes

heavy_obj = create_heavy_obj()

# do some jobs

del heavy_obj
ctypes.CDLL("libc.so.6").malloc_trim(0) 

Depending on your OS, it may not take back the released memory.

You can try out malloc_trim on Linux:

import ctypes

heavy_obj = create_heavy_obj()

# do some jobs

del heavy_obj
ctypes.CDLL("libc.so.6").malloc_trim(0) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文