python - 如何释放与 Cython 模块接口的外部 C 库分配的内存,其中内存最终返回给 Python 进程?
问题描述
我是 Cython 的新手,但基本上我有这个应用程序需要显着提高性能,所以我和我的团队正在尝试重写我们在 Cython 和 C 中的瓶颈。
对于我们应用程序中最慢的部分,我编写了一些 C 代码,这些代码被编译到库中并cdef extern
导入到 Cython 模块中,我认为这是一个.pyx
文件。本质上,pyx
文件中的代码基本上只是一个包装器,它返回对 C 库函数的调用。最后,有一个 Python 进程(主应用程序)导入pyx
文件中定义的所有函数并使用这些结果。
我相信我有内存泄漏,因为在 C 代码中,我需要传递给 Python 进程的结果有时是动态分配的。我的问题是,一旦 Python 进程使用了它,我不知道如何释放它。
示例 Python 代码
from examplecython import *
def foo(data):
context = data.context
value = call_pyx_function(context, data)
return value
def bar(results):
for data in results:
res = foo(data)
do_something_with_res(res)
# I want to free here
示例 Cython 代码
cdef extern from "my_lib.h"
char * my_function(const char * context, int data)
def call_pyx_function(context: bytes, int x):
return my_function(context, x)
示例 C 代码
#define BUFSIZE 256
char *
my_function(const char * context, int x) {
char * retbuf;
int res;
retbuf = (char *)malloc(BUFSIZE * sizeof(char));
res = do_some_math(x, context);
int length = snprintf(retbuf, BUFSIZE, "%d", res);
if (length >= BUFSIZE) {
exit(EXIT_FAILURE);
}
return retbuf;
}
如果有人对我如何以及在何处释放此内存有任何建议,将不胜感激。
解决方案
您可以直接free
从以下位置导入libc.stdlib
:
from libc.stdlib cimport free
def bar(results):
for data in results:
res = foo(data)
try:
do_something_with_res(res)
finally:
free(res)
(请注意,您需要,try/finally
因为即使某些东西引发异常,您也希望它被释放)
__del__
您可以使用上下文管理器或在/中删除的包装器使这更容易__dealloc__
:
@contextlib.contextmanager
def freeing(res):
try:
yield res
finally:
free(res)
def bar(results):
for data in results:
with freeing(foo(data)) as res:
do_something_with_res(res)
或者(可能会在很久以后被释放,可能会更慢,但(几乎)保证最终会被释放)
# (in pyx file)
cdef class MallocedResource:
cdef void* res;
def __init__(self, res):
# Note: This "steals" res. Don't free `res`
# as it is freed when this class's storage is freed
self.res = <void *>res
def __dealloc__(self):
free(self.res)
def call_pyx_function(context: bytes, int x):
return MallocedResouce(my_function(context, x))
# No need to change python code, so you can't forget to use try/finally.
推荐阅读
- reactjs - React 应用程序与服务人员显示以前的缓存文件
- css - 如何使一个 CSS 元素适合另一个?
- webpack - Webpack MiniCSSExtractPlugin:cacheGroups 取决于导入文件名
- sql - 无损连接分解属性 - BCNF 分解只有一个无损分解
- python - 如果列包含 Pandas 中的字符,则更改列类型
- cypress - 当 cypress 中的 DOM 中不存在元素时显示错误消息
- web-crawler - 提交的 URL 被 robots.txt 阻止
- vbscript - 经典的 asp 发布登录表单不再起作用
- laravel - 在 Laravel 5.7 中使用 Guzzle 6 在 Sage API 3.1 上工作时缺少请求正文
- jquery - Ajax 表单偶尔会将数据添加到 URL 而不是 POST 然后重定向