简体   繁体   English

将变量更改为0以在python中节省内存是个好主意吗?

[英]Is it good idea to change variable to 0 to save memory in python?

I've searched and couldn't find any good answer. 我已经搜索过,找不到任何好的答案。 Let's say that I have some list that have thousands of entries or something else that consumes a lot of memory. 假设我有一些包含数千个条目的列表,或者其他消耗大量内存的列表。 Is it good idea to change this list/etc. 更改此列表/等是个好主意。 value to 0... 值设为0 ...

oldlst = 0

...if it's no longer needed (or won't be updated in a very long time) or should I leave it alone in program. ...如果不再需要它(或者很长一段时间不会更新),或者我应该将它留在程序中。 Or maybe there is better way to handle this. 或者,也许有更好的方法来处理此问题。

Example: 例:

L = range(50*2000*1000)
raw_input("Press Enter")
L = 0
print L

When I run this command I use 838.1 MB of RAM after pressing enter and changing L to just 0 I release some of the memory taken 当我运行此命令时,按Enter键并将L更改为0后,我使用了838.1 MB RAM,我释放了一些占用的内存

You need to think on releasing memory rather than making list to 0 once not required. 您需要考虑释放内存,而不是一次不需要将列表设置为0。

Please refer 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM