nakama
nakama copied to clipboard
[Lua] - Locally scoped variables not gc-ed
Description
Using locally scoped variables in lua runtime are not being released correctly, thus memory of table keeps increasing overtime.
Provided Code
`local d = {} do local a = {} local b = 1 d['a'] = a d['b'] = b end
for k, v in pairs(d) do print(k, v) end collectgarbage() print("=====================") for k, v in pairs(d) do print(k, v) end`
Steps to Reproduce
- Run provided code in stock lua 5.1 interpreter in VS Code or by using ZeroBrane
- Observe that after collectgarbage call, table contains only key b
- Run same code in Docker version of the nakama ( Windows ( Docker Desktop ))
- Notice that after collectgarbage call, table contains both keys a,b and a hasn't been released.
Expected Result
Inner locally scoped variables are released as soon as they are no longer used and memory is retained correctly.
Actual Result
Memory persists and inner locally scoped variables are kept.
Context
- [ ] Unity - 2021.3.23f
Your Environment
- Nakama: 3.16.0
- Database: Cockroach 20.2
- Environment name and version: Docker Desktop
- Operating System and version: Win 10
I think this is related to yuin/gopher-lua, not specific to Nakama Server Framework. According to yuin/gopher-lua#miscellaneous-notes there are slightly differences between yuin/gopher-lua and lua
with the collectgarbage
method.
Just a few months ago, there was an context leak with coroutine lib https://github.com/yuin/gopher-lua/pull/438, which I have already fixed and been merged https://github.com/yuin/gopher-lua/commit/8ee9c41994b5d2c399b096de45be5d624ee31362. Base of my observation the memory will be freed at some point of time.
@deflinhec yes, this is specific to yuin/gopher-lua and partly to nakama and how it is using this lib. We could maybe close this but it would be than nice to extend lua docs and add best pratices when it comes to developing in sad runtime on what to take extra care. Like here for memory releasing time as it could impact session lenght/number of matches performed until "used" memory has been released.