python del不释放所有内存 [英] python del not freeing all the memory

查看:1234
本文介绍了python del不释放所有内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的python程序中,我使用pandas读取一个csv文件并存储在内存中:

In my python program, I use pandas to read a csv file and store in memory:

data = pandas.read_csv('data.csv')

在运行上述命令之前,我用free -m检查可用内存,输出为1704.运行上述命令后,输出为729.我跑

Before running the above command I check the free memory with free -m and the output is 1704. After running the above command the output is 729. I run

del(data)

释放data所使用的内存.现在,当我检查可用内存时,输出为1093,它比原始的1704小得多.剩下的去了哪里?我该如何释放它?我正在ipython中运行所有这些命令,甚至退出ipython也无法释放该内存.

to free the memory used by data. Now when I check the free memory the output is 1093 which is much less than the original 1704. Where did the rest go? How can I free it? I'm running all these in ipython and even exiting ipython doesn't free up that memory.

谢谢.

推荐答案

退出ipython不会释放内存"意味着您正在OS级别看到效果.您可能会看到内存缓存的效果.数据将一直保存在内存中,直到需要空间或再次读取文件为止-而不是从磁盘读取文件,而是从内存的非活动"部分读取数据.

"Exiting ipython doesn't free up that memory" means that you're seeing the effect at the OS level. You're probably seeing the effect of memory caching. That data is saved in memory until the space is needed or the file is read again - instead of reading the file from disk it will be read from the 'inactive' section of memory.

假设您使用的是unix/linux变体,以了解有关ram使用的详细信息

Assuming you're on some unix/linux variant, for details about ram usage

cat /proc/meminfo

您的可用内存为Free + Inactive. Free现在可用,并且操作系统将在必要时从Inactive回收空间.我保留以下别名(在csh中)进行检查

Your available memory is Free + Inactive. The Free is available now, and the OS will reclaim space from Inactive as necessary. I keep the following alias (in csh) around to check

alias 'freemem' cat /proc/meminfo | grep -i 'inactive:\|memfree:'

Python的del将从名称空间中删除您的变量.因此,如果未在代码中的其他任何地方引用该内存,则python的垃圾回收将启动并从内存中清除数据.或者,如果您想强制使用它:

Python's del will remove your variable from the namespace. So if that memory isn't referenced anywhere else in your code, python's garbage collection will kick in and clear data from memory. Or if you want to force it:

import gc
foo = range(10000000)
del foo
gc.collect()

无论如何,操作系统都会保留文件内容的高速缓存,因此当您再次尝试读取同一文件时,它不必进行冗余磁盘读取.

Regardless of all this, the OS will keep the file contents cached so it doesn't have to do redundant disk reads when you try to read that same file again.

这篇关于python del不释放所有内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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