gpt4 book ai didi

python-pyramid 应用程序内 stub 本没有释放

转载 作者:太空狗 更新时间:2023-10-29 21:58:32 25 4
gpt4 key购买 nike

如何解决这个内存泄漏问题?

我应该采取什么措施来清理旧的 session 对象?session.close() 还不够吗?

和 Pyramid 有关吗?

Sqlalchmey setup:
----------------------------------------------------------------------------------
def get_db(request):
maker = request.registry.dbmaker
session = maker()

@profile
def cleanup(request):
_session = request.db
if request.exception is not None:
_session.rollback()
else:
_session.commit()
_session.close()
# del _session # No memory released

request.add_finished_callback(cleanup)
return session

def main(global_config, **settings):
:
:
config.registry.dbmaker = sessionmaker(bind=engine)
config.add_request_method(get_db, name='db', reify=True)
:
:

Pyramid 应用程序请求处理程序就像

@view_config(route_name='list_employees', renderer='json')
def employees(request):
session = request.db
office = session.query(Office).get(1)
employees = [x.name for x in office.employees]
return employees

现在的问题是,在每个对 list_employees 的请求中,内存都在增长。内存增加的大小几乎等于 office.employees.

的大小

调试:

request 1 starts with memory utilization = 10MB
request 1 ends with memory utilization = 18MB

request 2 starts with memory utilization = 18MB
request 2 ends with memory utilization = 26MB

request 3 starts with memory utilization = 26MB
request 3 ends with memory utilization = 34MB
:
:
Grows eventually

employees = [x.name for x in office.employees]
This is the line where about 8-10MB memory utilized

为了调试,我在 Employ 和 Office 模型中添加了 __del__ 方法,看起来它们正在删除。

还尝试了 session.expunge(office)del officegc.collect()

我正在使用 https://pypi.python.org/pypi/memory_profiler 调试内存消耗我也在使用 https://pypi.python.org/pypi/transaction是其他请求。

不使用调试 Pyramid 工具栏。

编辑:发现此行的内存增加(employees = [x.name for x in office.employees])在 6-7 次请求后显示为零。但查询返回的行数相同。

编辑:添加了独立应用程序 https://github.com/Narengowda/pyramid_sqlalchemy_app

编辑:它与 SQLALCHEMY 完全无关(我的错)。编写了一个没有任何 sqlalchmey 查询的简单 View 函数。

class Test(object):

def __init__(self):
self.x = 'sdfklhasdjkfhasklsdkjflksdfksd' *1000
self.y = 'sdfklhasdjkfhasklsdkjflksdfksd' *1000
self.z = 'sdfklhasdjkfhasklsdkjflksdfksd' *1000
self.i = 'sdfklhasdjkfhasklsdkjflksdfksd' *1000
self.v = 'sdfklhasdjkfhasklsdkjflksdfksd' *1000
self.o = 'sdfklhasdjkfhasklsdkjflksdfksd' *1000


@view_config(route_name='home', renderer='json')
def my_view(request):
return test(request)

@profile
def test(request):
count = request.GET.get('count')
l = [Test() for i in range(int(count))]
print l[0]
return {}

我能看到这个,下面是请求的日志

请求:1

Line # Mem usage Increment Line Contents


23     37.3 MiB      0.0 MiB   @profile
24 def test(request):
25 37.3 MiB 0.0 MiB count = request.GET.get('count')
26 112.4 MiB 75.1 MiB l = [Test() for i in range(int(count))]
27 112.4 MiB 0.0 MiB print l[0]
28 112.4 MiB 0.0 MiB return {}

请求:2

Line # Mem usage Increment Line Contents


23    111.7 MiB      0.0 MiB   @profile
24 def test(request):
25 111.7 MiB 0.0 MiB count = request.GET.get('count')
26 187.3 MiB 75.6 MiB l = [Test() for i in range(int(count))]
27 187.3 MiB 0.0 MiB print l[0]
28 187.3 MiB 0.0 MiB return {}

请求:3

Line # Mem usage Increment Line Contents


23    184.3 MiB      0.0 MiB   @profile
24 def test(request):
25 184.3 MiB 0.0 MiB count = request.GET.get('count')
26 259.7 MiB 75.4 MiB l = [Test() for i in range(int(count))]
27 259.7 MiB 0.0 MiB print l[0]
28 259.7 MiB 0.0 MiB return {}

请求:4

Line # Mem usage Increment Line Contents


23    255.1 MiB      0.0 MiB   @profile
24 def test(request):
25 255.1 MiB 0.0 MiB count = request.GET.get('count')
26 330.4 MiB 75.3 MiB l = [Test() for i in range(int(count))]
27 330.4 MiB 0.0 MiB print l[0]
28 330.4 MiB 0.0 MiB return {}

请求:5

Line # Mem usage Increment Line Contents


23    328.2 MiB      0.0 MiB   @profile
24 def test(request):
25 328.2 MiB 0.0 MiB count = request.GET.get('count')
26 330.5 MiB 2.3 MiB l = [Test() for i in range(int(count))]
27 330.5 MiB 0.0 MiB print l[0]
28 330.5 MiB 0.0 MiB return {}

请求:6

Line # Mem usage Increment Line Contents


23    330.5 MiB      0.0 MiB   @profile
24 def test(request):
25 330.5 MiB 0.0 MiB count = request.GET.get('count')
26 330.5 MiB 0.0 MiB l = [Test() for i in range(int(count))]
27 330.5 MiB 0.0 MiB print l[0]
28 330.5 MiB 0.0 MiB return {}

我用不同的计数查询参数尝试了很多次,发现内存利用率的增加在恰好 5 个请求后停止(神奇)。

我还尝试打印所有对象并比较那里的地址我观察到的是查看请求 4 和 5 的日志。看起来 GC 发生了,因此内存从 330.4 Mi 减少到 328.2 MiB但是您不会看到 75.3 MiB 内存使用率来创建新对象(第 26 行),但您只会看到 2.3 MiB 的增加。后来我验证了前两次请求中创建的对象地址,发现前两次请求中80%的对象地址是相同的

请求:4个对象地址

<pyramid_sqa.views.Test object at 0x3a042d0>
<pyramid_sqa.views.Test object at 0x3a04310>
<pyramid_sqa.views.Test object at 0x3a04350>
<pyramid_sqa.views.Test object at 0x3a04390>
<pyramid_sqa.views.Test object at 0x3a043d0>
<pyramid_sqa.views.Test object at 0x3a04410>
<pyramid_sqa.views.Test object at 0x3a04450>
<pyramid_sqa.views.Test object at 0x3a04490>
<pyramid_sqa.views.Test object at 0x3a044d0>
<pyramid_sqa.views.Test object at 0x3a04510>

请求:5 个对象地址

<pyramid_sqa.views.Test object at 0x3a04390>
<pyramid_sqa.views.Test object at 0x3a043d0>
<pyramid_sqa.views.Test object at 0x3a04410>
<pyramid_sqa.views.Test object at 0x3a04450>
<pyramid_sqa.views.Test object at 0x3a04490>
<pyramid_sqa.views.Test object at 0x3a044d0>
<pyramid_sqa.views.Test object at 0x3a04290>
<pyramid_sqa.views.Test object at 0x3a04550>
<pyramid_sqa.views.Test object at 0x3a04590>
<pyramid_sqa.views.Test object at 0x3a045d0>

因此创建了新对象并且 python 正在重用内存(重用对象!!?)

如果我的服务器内存像这样猛增,可以吗?

最佳答案

Python 对 Python 对象进行自己的内存管理,即使 CPython GC 释放了 Python 对象,它仍然不会将内存释放给操作系统(就像 malloc()/free() 可能做的那样)。当 GC 释放一个 Python 对象时,内存可以用于新的 Python 对象。这是您在请求编号 6 的内存消耗没有增加时看到的效果。在编号 5 之后,GC 释放了已删除的对象,请求编号 6 中的新对象可以使用释放的内存。

所以你没有内存泄漏,你只是发现了 CPython 内存管理的工作原理。内存消耗不会无限制地增长。

关于python-pyramid 应用程序内 stub 本没有释放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25200475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com