python开发的程序内存越来越大_为什么我的python进程占用了这么多内存?

在处理大型文件时,作者发现Python程序内存使用率高达60%,但实际变量总大小仅为23Mb。文章通过代码展示了读取和处理文件的过程,探讨了内存管理问题,包括释放内存、文件操作和内存监测。尽管尝试将不再使用的变量设为None,内存占用仍然高企,提示可能存在内存泄漏或未有效释放资源的问题。
摘要由CSDN通过智能技术生成

I'm working on a project that involves using python to read, process and write files that are sometimes as large as a few hundred megabytes. The program fails occasionally when I try to process some particularly large files. It does not say 'memory error', but I suspect that is the problem (in fact it gives no reason at all for failing').

I've been testing the code on smaller files and watching 'top' to see what memory usage is like, and it typically reaches 60%. top says that I have 4050352k total memory, so 3.8Gb.

Meanwhile I'm trying to track memory usage within python itself (see my question from yesterday) with the following little bit of code:

mem = 0

for variable in dir():

variable_ = vars()[variable]

try:

if str(type(variable_))[7:12] == 'numpy':

numpy_ = True

else:

numpy_ = False

except:

numpy_ = False

if numpy_:

mem_ = variable_.nbytes

else:

mem_ = sys.getsizeof(variable)

mem += mem_

print variable+ type: '+str(type(variable_))+' size: '+str(mem_)

print 'Tot

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值