linux中python不自动释放内存吗_linux上没有释放Python内存?

在Linux环境中,Python在读取并解析大型JSON文件后,内存占用并未如预期般下降。即使删除了相关变量并调用了垃圾回收器,内存仍然保持高位。相比之下,Windows环境下内存会正常释放。此现象可能与Python的内存管理机制和系统差异有关。
摘要由CSDN通过智能技术生成

我试图将一个大型json对象加载到内存中,然后对数据执行一些操作。但是,我注意到在读取json文件之后,RAM有了很大的增加-即使对象超出了范围。

这是密码import json

import objgraph

import gc

from memory_profiler import profile

@profile

def open_stuff():

with open("bigjson.json", 'r') as jsonfile:

d= jsonfile.read()

jsonobj = json.loads(d)

objgraph.show_most_common_types()

del jsonobj

del d

print ('d')

gc.collect()

open_stuff()

我尝试在Windows中运行这个脚本,使用Python2.7.12版本,在Debian9中运行Python2.7.13版本,我发现Linux中的Python有一个问题。在

在Windows中,当我运行脚本时,它会在json对象被读取和作用域内(如预期的那样)占用大量的RAM,但是在操作完成后释放它(如预期的那样)。在

^{pr2}$

然而,在LINUX环境中,尽管对JSON对象的所有引用都已删除,但仍使用超过500MB的RAM。在list 3039186

dict 413836

function 2336

wrapper_descriptor 1193

builtin_function_or_method 765

method_descriptor 651

tuple 514

weakref 480

property 273

member_descriptor 250

d

Filename: testjson.py

Line # Mem usage Increment Line Contents

================================================

5 14.2 MiB 14.2 MiB @profile

6 def open_stuff():

7 14.2 MiB 0.0 MiB with open("bigjson.json", 'r') as jsonfile:

8 195.1 MiB 181.0 MiB d= jsonfile.read()

9 1466.4 MiB 1271.3 MiB jsonobj = json.loads(d)

10 1466.8 MiB 0.4 MiB objgraph.show_most_common_types()

11 694.8 MiB -772.1 MiB del jsonobj

12 513.8 MiB -181.0 MiB del d

13 513.8 MiB 0.0 MiB print ('d')

14 513.0 MiB -0.8 MiB gc.collect()

在Debian9和Python3.5.3中运行的同一个脚本使用较少的RAM,但泄漏了相应数量的RAM。在list 3039266

dict 414638

function 3374

tuple 1254

wrapper_descriptor 1076

weakref 944

builtin_function_or_method 780

method_descriptor 780

getset_descriptor 477

type 431

d

Filename: testjson.py

Line # Mem usage Increment Line Contents

================================================

5 17.2 MiB 17.2 MiB @profile

6 def open_stuff():

7 17.2 MiB 0.0 MiB with open("bigjson.json", 'r') as jsonfile:

8 198.3 MiB 181.1 MiB d= jsonfile.read()

9 1057.7 MiB 859.4 MiB jsonobj = json.loads(d)

10 1058.1 MiB 0.4 MiB objgraph.show_most_common_types()

11 537.5 MiB -520.6 MiB del jsonobj

12 356.5 MiB -181.0 MiB del d

13 356.5 MiB 0.0 MiB print ('d')

14 355.8 MiB -0.8 MiB gc.collect()

是什么导致了这个问题?

两个版本的Python都运行64位版本。在

EDIT-连续多次调用该函数会导致更奇怪的数据json.loads函数每次调用时使用较少的RAM,第三次尝试后RAM使用稳定,但之前泄漏的RAM不会释放。list 3039189

dict 413840

function 2339

wrapper_descriptor 1193

builtin_function_or_method 765

method_descriptor 651

tuple 517

weakref 480

property 273

member_descriptor 250

d

Filename: testjson.py

Line # Mem usage Increment Line Contents

================================================

5 14.5 MiB 14.5 MiB @profile

6 def open_stuff():

7 14.5 MiB 0.0 MiB with open("bigjson.json", 'r') as jsonfile:

8 195.4 MiB 180.9 MiB d= jsonfile.read()

9 1466.5 MiB 1271.1 MiB jsonobj = json.loads(d)

10 1466.9 MiB 0.4 MiB objgraph.show_most_common_types()

11 694.8 MiB -772.1 MiB del jsonobj

12 513.9 MiB -181.0 MiB del d

13 513.9 MiB 0.0 MiB print ('d')

14 513.1 MiB -0.8 MiB gc.collect()

list 3039189

dict 413842

function 2339

wrapper_descriptor 1202

builtin_function_or_method 765

method_descriptor 651

tuple 517

weakref 482

property 273

member_descriptor 253

d

Filename: testjson.py

Line # Mem usage Increment Line Contents

================================================

5 513.1 MiB 513.1 MiB @profile

6 def open_stuff():

7 513.1 MiB 0.0 MiB with open("bigjson.json", 'r') as jsonfile:

8 513.1 MiB 0.0 MiB d= jsonfile.read()

9 1466.8 MiB 953.7 MiB jsonobj = json.loads(d)

10 1493.3 MiB 26.6 MiB objgraph.show_most_common_types()

11 723.9 MiB -769.4 MiB del jsonobj

12 723.9 MiB 0.0 MiB del d

13 723.9 MiB 0.0 MiB print ('d')

14 722.4 MiB -1.5 MiB gc.collect()

list 3039189

dict 413842

function 2339

wrapper_descriptor 1202

builtin_function_or_method 765

method_descriptor 651

tuple 517

weakref 482

property 273

member_descriptor 253

d

Filename: testjson.py

Line # Mem usage Increment Line Contents

================================================

5 722.4 MiB 722.4 MiB @profile

6 def open_stuff():

7 722.4 MiB 0.0 MiB with open("bigjson.json", 'r') as jsonfile:

8 722.4 MiB 0.0 MiB d= jsonfile.read()

9 1493.1 MiB 770.8 MiB jsonobj = json.loads(d)

10 1493.4 MiB 0.3 MiB objgraph.show_most_common_types()

11 724.4 MiB -769.0 MiB del jsonobj

12 724.4 MiB 0.0 MiB del d

13 724.4 MiB 0.0 MiB print ('d')

14 722.9 MiB -1.5 MiB gc.collect()

Filename: testjson.py

Line # Mem usage Increment Line Contents

================================================

17 14.2 MiB 14.2 MiB @profile

18 def wow():

19 513.1 MiB 498.9 MiB open_stuff()

20 722.4 MiB 209.3 MiB open_stuff()

21 722.9 MiB 0.6 MiB open_stuff()

编辑2:有人建议这是Why does my program's memory not release?的副本,但所讨论的内存量与另一个问题中讨论的“小页面”相差甚远。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值