使用 python-behave 时内存使用率高

High memory usage when using python-behave

我 运行 在使用 python-behave 时遇到问题。

我在每个场景之前创建一个沙箱数据库,并在场景之后销毁它。

但每个场景的内存使用量增加约 20MB,所有测试用例 (LoL) 的总使用量约为 3.xGB。

我的问题是为什么当我调用 context.runner.teardown_databases() 时内存没有释放?

        from django.test.runner import DiscoverRunner
        def before_scenario(context, scenario):
            context.runner = DiscoverRunner()
            context.runner.setup_test_environment()
            context.old_db_config = context.runner.setup_databases()

        def after_scenario(context, scenario):
            context.runner.teardown_databases(context.old_db_config)
            context.runner.teardown_test_environment()

    Feature:
      Scenario: 
        Given I have a debit card
        When I withdraw 200 
        Then I should get 0 in cash
    @given('I have a debit card')
    def step1(context):
        pass
    @given('I withdraw 200')
    def step2(context):
        pass
    @given('I should get 0 in cash')
    def step3(context):
        pass

python-行为:版本 1.2.5

django:版本 1.8.0

python:版本 2.7

如有任何建议,我们将不胜感激。

添加 gc.garbage() 后,我找到了根本原因。

def after_scenario(context, scenario):
    context.runner.teardown_databases(context.old_db_config)
    context.runner.teardown_test_environment()
    gc.collect()
    print gc.garbage

那些不可收集的对象应该是模型实例,因为这些实例是循环引用的。 然后我添加了删除thmem的purge功能,现在内存正常了