Skip to content
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

Closed
jonas0616 opened this issue Dec 3, 2015 · 11 comments
Closed

Memory leak for json encode #6

jonas0616 opened this issue Dec 3, 2015 · 11 comments

Comments

@jonas0616
Copy link

#!/usr/bin/env luajit
local json = require "resty.libcjson"
local tbl  = {hello = "world"}

for _ = 1, 100000000 do
    local json_str = json.encode(tbl)
end

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:

$ luajit -v
LuaJIT 2.0.2 -- Copyright (C) 2005-2013 Mike Pall. http://luajit.org/
$ luarocks list lua-resty-libcjson

Installed rocks:

lua-resty-libcjson
1.1-1 (installed) - /usr/local/lib/luarocks/rocks

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.

@bungle
Copy link
Owner

bungle commented Dec 3, 2015

Yes, the encode is like total mess with this. On this more up-to-date version there is:
kbranigan/cJSON@dbd0a70
and
kbranigan/cJSON@601a307

@bungle
Copy link
Owner

bungle commented Dec 3, 2015

Hi, I added small patch, can you check if it helps, also try out kbranigan/cJSON.

@bungle
Copy link
Owner

bungle commented Dec 3, 2015

I already checked, and it doesn't help.

@ngo
Copy link

ngo commented Apr 22, 2016

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.

@bungle
Copy link
Owner

bungle commented Apr 23, 2016

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

This was referenced Apr 23, 2016
@bungle
Copy link
Owner

bungle commented Apr 25, 2016

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.

@someburner
Copy link

Ah, looks like the reply by corsix may be the issue. For instance in my C code I have to do:

    char * json_string;
    json_string = cJSON_Print(root);

    int ret = http_write(c,json_string);
    os_free(json_string);

where root is also a malloc'd cJSON object.

@bungle
Copy link
Owner

bungle commented Apr 25, 2016

@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 bungle closed this as completed Apr 25, 2016
@ngo
Copy link

ngo commented Apr 25, 2016

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

@ngo
Copy link

ngo commented Apr 26, 2016

The long test also doesn't show any signs of leaks

@bungle
Copy link
Owner

bungle commented Apr 26, 2016

Thanks @ngo for checking and confirming that the fix had the desired effect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants