2016-09-30 5 views
0

この回答(https://groups.google.com/forum/#!msg/torch7/rmIcBYCiFG8/IC68Xzd3DgAJ)によれば、トーチは自動的に使用されなくなったテンソルを自動的に解放します。そして私は既にこの問題をテンソルポインタとしていましたが、自動的にluaによって解放され、cプログラムでセグメンテーションが発生しました。しかし、私の実験では、手動でCコードをフリーにしても、プログラムメモリが絶えず増加しているメモリリークが発生するようです。ここでluaはCからプッシュされたトーチのテンソルを収集しますか?

は私のテストコードです:

#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でメモリを管理する方法は?

+0

これはCコードではありません。 – Olaf

+0

私は知っていますが、問題はcライブラリ(luaT.h、lua.h)に関連しています。この例のcpp部分は、この問題とは関係ありません。 – Conchylicultor

答えて

0

私はあなたがあるため、この行で "tensordata" フロートtensorData =(フロート)はmalloc(はsizeof(float型)* LEN)用のメモリの割り当てを解除しないとメモリリークを取得していると思います。

希望します。

+0

このメモリは、THFloatStorageが解放されたときにデスオラレーションされます:https://github.com/torch/torch7/blob/master/lib/TH/generic/THStorage.c#L120問題は、それが紛争whith lua garbadgeコレクタを作成することですテンソルを削除する(2回削除==トラブル)、segfaultを作成する。 – Conchylicultor

関連する問題