首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我的PyV8上下文泄漏内存

我的PyV8上下文泄漏内存
EN

Stack Overflow用户
提问于 2014-03-29 03:23:34
回答 1查看 247关注 0票数 1

无论我尝试使用PyV8,我总是会发现大量的内存泄漏。即使在使用空字符串执行evals时,它似乎仍然以某种方式泄漏内存。在下面发布的示例中,使用空字符串执行1000万次eval会生成320 of的内存,在之后显式调用垃圾收集器时只会收集其中的20 of。无论上下文是可访问的还是不可访问的,对我来说似乎没有什么区别。我用类似的测试测试了我的python本身,它不会泄漏内存。我做错了什么吗?

版本

PyV8修订版557,使用PyV8 8的setup.py构建在同一台机器上

V8修订版19632,使用PyV8 8的setup.py构建在同一台机器上

操作系统: Ubuntu 12.04

测试代码

代码语言:javascript
复制
import unittest, gc, os, logging as python_logging
from PyV8 import JSEngine, JSContext

_proc_status = '/proc/%d/status' % os.getpid()
_scale = {'kB': 1024.0, 'mB': 1024.0*1024.0,
          'KB': 1024.0, 'MB': 1024.0*1024.0}

def log_memory_usage(intro_text=""):
    python_logging.info(
        (
            '%s process %d now uses %.1f MB resident'
                %(intro_text, os.getpid(), resident()/(1024*1024))
        ).strip()
    )

def _VmB(VmKey):
    '''Private.
    '''
    global _proc_status, _scale
     # get pseudo file  /proc/<pid>/status
    try:
        t = open(_proc_status)
        v = t.read()
        t.close()
    except:
        return 0.0  # non-Linux?
     # get VmKey line e.g. 'VmRSS:  9999  kB\n ...'
    i = v.index(VmKey)
    v = v[i:].split(None, 3)  # whitespace
    if len(v) < 3:
        return 0.0  # invalid format?
     # convert Vm value to bytes
    return float(v[1]) * _scale[v[2]]

def resident(since=0.0):
    '''Return resident memory usage in bytes.
    '''
    return _VmB('VmRSS:') - since   

class TestMemoryWithJSContext(unittest.TestCase):
  def test_python_memory_management(self):
    def inner():
        with JSContext() as ctx:
            log_memory_usage("before empty evals")
            for index1 in range(1000):
                for index2 in range(10000):
                    ctx.eval("")
            log_memory_usage("after empty evals")
            JSEngine.collect()
    log_memory_usage("before JSContext memory tests")
    inner()
    JSEngine.collect()
    gc.collect()
    JSEngine.collect()
    gc.collect()
    log_memory_usage("after JSContext memory tests and gc")
    print "Py gc.garbage:", gc.garbage

class CardEngineTestSuite(unittest.TestSuite):
    def __init__(self):
        super(CardEngineTestSuite, self).__init__()
        self.addTests(unittest.TestLoader().loadTestsFromTestCase(TestPython))
        self.addTests(unittest.TestLoader().loadTestsFromTestCase(TestMemoryWithJSContext))

if __name__ == '__main__':
    python_logging.basicConfig(level=python_logging.INFO, format='%(asctime)s %(message)s')
    unittest.TextTestRunner().run(CardEngineTestSuite())
    unittest.TextTestRunner().run(CardEngineTestSuite())
    python_logging.info(str(gc.garbage))

输出

代码语言:javascript
复制
.2014-03-28 21:41:34,198 before JSContext memory tests process 110 now uses 14.1 MB resident
2014-03-28 21:41:34,199 before empty evals process 110 now uses 14.4 MB resident
2014-03-28 21:41:55,513 after empty evals process 110 now uses 348.8 MB resident
2014-03-28 21:41:56,926 after JSContext memory tests and gc process 110 now uses 322.3 MB resident
Py gc.garbage: []
.
----------------------------------------------------------------------
Ran 2 tests in 26.838s

OK
.2014-03-28 21:42:01,103 before JSContext memory tests process 110 now uses 322.5 MB resident
2014-03-28 21:42:01,104 before empty evals process 110 now uses 322.5 MB resident
2014-03-28 21:42:25,714 after empty evals process 110 now uses 636.5 MB resident
2014-03-28 21:42:28,459 after JSContext memory tests and gc process 110 now uses 629.3 MB resident
Py gc.garbage: []
.
----------------------------------------------------------------------
Ran 2 tests in 31.532s

OK
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-19 02:12:54

下面的PyV8票据中描述的答案为我解决了这个问题--尽管垃圾收集现在占用了大量的CPU时间。为了解决这个问题,我将我的PyV8代码移到芹菜工人中,让它在后台进行垃圾收集。如果你想看到这个代码,现在我真的没时间准备示例代码了。

https://code.google.com/p/pyv8/issues/detail?id=229&sort=-id

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22726593

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档