nakama icon indicating copy to clipboard operation
nakama copied to clipboard

[Lua] - Locally scoped variables not gc-ed

Open AleksandarEri opened this issue 1 year ago • 2 comments

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

  1. Run provided code in stock lua 5.1 interpreter in VS Code or by using ZeroBrane
  2. Observe that after collectgarbage call, table contains only key b
  3. Run same code in Docker version of the nakama ( Windows ( Docker Desktop ))
  4. 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

AleksandarEri avatar May 24 '23 12:05 AleksandarEri

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 avatar Jul 19 '23 08:07 deflinhec

@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.

AleksandarEri avatar Jul 19 '23 09:07 AleksandarEri