gvvaughan / lyaml

LibYAML binding for Lua.
gvvaughan.github.io/lyaml
Other
208 stars 34 forks source link

memory leak? #35

Closed jayce closed 1 year ago

jayce commented 4 years ago

Hi, I found a problem, looks like memory leak, in lua-5.1.4. see the following image and code.

screenshot: image

the code:

local lyaml = require("lyaml")

local pok, tb = pcall(lyaml.load, "_format_version: '1.1'")
for i = 1, 1000000000 do
local yaml, err = lyaml.dump({tb})
end

and the mem of flamegraph, maybe the problem is Pemitter() or emit(): image

freefengeli commented 3 years ago

I also found this problem, how to solve it?

gvvaughan commented 3 years ago

Honestly, I don’t know how to reproduce it or debug it. I doubt that any Lua code is leaking, although if it only happens with Lua 5.1 and a modern Lua that is a possibility. Or maybe lyaml is leaking? Or it must be the C code here in lyaml.

If you can describe a reliable way to reproduce the leak I’ll at least have somewhere to start. If you want to investigate and propose a fix, that would definitely be the fastest way to remedy the problem.

freefengeli commented 3 years ago

The code is as follows:

local yaml=require("lyaml")
print(collectgarbage("count"),"kb")
for i=1,10000 do
  yaml.dump({{a="b"}})
end
print(collectgarbage("count"),"kb")
collectgarbage()
print(collectgarbage("count"),"kb")

The printing results are as follows: 104.9541015625 kb 8414.7314453125 kb 5820.7744140625 kb

About 5M of memory is not recycled