【问题标题】:Does lua garbage collect torch tensors pushed from C?lua 垃圾会收集从 C 推出的火炬张量吗?
【发布时间】:2017-02-09 10:38:17
【问题描述】:

根据这个答案(https://groups.google.com/forum/#!msg/torch7/rmIcBYCiFG8/IC68Xzd3DgAJ),torch 似乎自动释放了不再使用的张量。我已经遇到了这个问题,张量指针被 lua 自动释放,导致 c 程序出现段错误。但是,从我的实验来看,似乎没有在 C 代码上手动调用 free 会导致内存泄漏,即程序内存不断增加。

这是我的测试代码:

#include <iostream>
#include <chrono>
#include <thread>

#include <lua.hpp>
extern "C" {
#include <TH.h>
#include <luaT.h>
}

using namespace std;

int main(int argc, char** argv)
{
    lua_State* L = luaL_newstate();

    luaL_openlibs(L);

    lua_getglobal(L, "require");
    lua_pushstring(L, "torch");
    lua_pcall(L,1,0,0); // require "torch"

    int h=224, w=224, nb_channel=3;

    int len = h * w * nb_channel;
    long stride_1 = h * w;
    long stride_2 = w;
    long stride_3 = 1;

    for (size_t i = 0 ; i < 1000000 ; ++i)
    {
        float *tensorData = (float*)malloc(sizeof(float)*len); // Should be when the storage is freed
        THFloatStorage *storage = THFloatStorage_newWithData(tensorData, len); // Memory released when tensor input is freed by lua garbadge collector
        THFloatTensor* input = THFloatTensor_newWithStorage3d(storage,
                                                              0, // Offset
                                                              nb_channel, stride_1, // Channels
                                                              h,          stride_2, // Height
                                                              w,          stride_3); // Width
        // Do some initialisation of the tensor...

        luaT_pushudata(L, (void*) input, "torch.FloatTensor"); // Send the tensor to lua
        // Do some stuff with the input... (call lua torch scripts...)
        lua_pop(L, 1);

        // If those two lines is not set, we get a memory leak
        THFloatTensor_free(input); // Will  sometimes create a segfault as the tensor seems to be deleted by the lua garbadge collector
        THFloatStorage_free(storage);

        if (i%10000 == 0)
            std::cout << i << std::endl;
        std::this_thread::sleep_for(std::chrono::microseconds(1));
    }



    return 0;
}

如何用c和lua正确管理内存,避免segfault又不造成内存泄漏?

【问题讨论】:

  • 那不是 C 代码。
  • 我知道,但问题与 c 库(luaT.h、lua.h)有关。示例的 cpp 部分与问题无关。

标签: c++ lua torch


【解决方案1】:

我认为您因为没有在这一行中为“tensordata”释放内存而导致内存泄漏 浮动 tensorData = (float)malloc(sizeof(float)*len);

希望对你有帮助

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-24
  • 2023-04-04
  • 2010-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多