-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Memory leak for json encode #6
Comments
Yes, the encode is like total mess with this. On this more up-to-date version there is: |
Hi, I added small patch, can you check if it helps, also try out kbranigan/cJSON. |
I already checked, and it doesn't help. |
This leak is actually pretty bad. In my experience under moderate load (<100 rps), when doing one encode per request this can easily leak 100-150 megs per worker per day. I had to switch to pure-lua lunajson lib, which doesn't leak. |
@ngo, Yeah, I think the leak is in C-module, and there is little I can do. On OpenResty the bundled Lua cJSON is the preferred lib, which is different than this). |
If I change code to this: function json.encode(value, formatted)
local j = ffi_gc(json.encval(value), cjson.cJSON_Delete)
if j == nil then return nil end
print(collectgarbage("count")*1024)
collectgarbage()
collectgarbage()
return formatted ~= false and ffi_str(cjson.cJSON_Print(j)) or ffi_str(cjson.cJSON_PrintUnformatted(j))
end I get 46465 printed constantly, but the memory grows. So this might be actually a memory leak in LuaJIT. |
Ah, looks like the reply by corsix may be the issue. For instance in my C code I have to do:
where root is also a malloc'd cJSON object. |
@someburner I just committed this as 1.2 and after some cleanups 1.3 that fixes this issue. Thanks a lot. @ngo, @jonas0616, will you also check if it is fixed now, if not, please reopen this issue. Thanks for reporting. |
@bungle Great work, thanks! My short test (~30 min) shows that the memory leak is gone. I will update once I collect more info overnight. |
The long test also doesn't show any signs of leaks |
Thanks @ngo for checking and confirming that the fix had the desired effect. |
Run the above code and observe. The memory usage keeps growing during repeated json encode.
However, json decode works just fine.
The version of luajit and lua-resty-libcjson is:
And cJSON is download from http://sourceforge.net/projects/cjson/files/latest/download
This issue also can be reproduced by openresty 1.7.10.2.
The text was updated successfully, but these errors were encountered: