sol2 icon indicating copy to clipboard operation
sol2 copied to clipboard

Memory leakage in linux using sol:: table

Open beyondkyq opened this issue 3 years ago • 0 comments

SOL_VERSION_STRING "3.2.3"    

C++:

typedef std::shared_ptr<sol::state> lua_state_ptr;
lua_state_ptr pState = nullptr;
sol::table TestFun(sol::this_state s)
{
    sol::state_view lua(s);
    sol::table points = lua.create_table();

    for (int i =0 ;i<100000;++i)
    {
	    sol::table tt= lua.create_table();
	    tt["datetime"] = 1;
	    tt["ms"] = 1;
	    tt["value"] = 1;
	    tt["quality"] = 1;
	    points.add(tt);
    }
    return points;
}
int main()
{
    try
        {
            pState = std::make_shared<sol::state>();
            pState->open_libraries();
            pState->set_function("TestFun", &TestFun0);
            auto varRet = pState->safe_script_file("/root/test.lua");
            if (varRet.valid())
            {
                   pState->script("yun()");  
            }
            pState->collect_garbage();
            pState->collect_garbage();
        }
        catch (sol::error& e)
        {
        }          
        return 0;
}

lua

    function yun()
        local mytable = TestFun()
        print(collectgarbage("count"))
        mytable = nil
        print(collectgarbage("count"))
        collectgarbage("collect")
        collectgarbage("collect")
        print(collectgarbage("count"))
    end

When this code runs on windows, the code executes to return points;, memory will grow to 45M; When executing to pstate - > collect_ garbage(); the memory will be reduced to 10M. However, when running on Linux, the code executes to return points;, The memory will grow to 27M and execute to pstate - > collect_ garbage();the memory will not be reduced. If multiple sol:: state are used in my system, the memory will grow to a very large size. Is my use inappropriate?

beyondkyq avatar Apr 10 '22 12:04 beyondkyq