did i need use the collectgarbage?

21 views
Skip to first unread message

leon liu

unread,
Apr 4, 2012, 8:51:33 AM4/4/12
to JNLua Discussion
sometime i need to alloc a lot of javaobject in the lua, i have no
idea what time the javaobject will be collect, for example:

function testmem ()
local StringBuilder = java.require("java.lang.StringBuilder")

for j = 1, 100 do
for i = 1, 1000000 do
local sbb = StringBuilder:new()
end
-- collectgarbage("collect") -- may be i need add this?
end
end

Andre Naef

unread,
Apr 4, 2012, 5:38:59 PM4/4/12
to jnlua-...@googlegroups.com
Collecting Java objects in JNLua is a two-step process:

First, the Lua garbage collector must collect the proxy userdata in Lua.

Second, the Java garbage collector must collect the actual Java objects.

Invoking collectgarbage("collect") triggers the first step.

leon liu

unread,
Apr 4, 2012, 6:49:56 PM4/4/12
to JNLua Discussion
oh, that's why i get the outofmemory at last, because i do the first
step only, is the second step to call LuaState.close() ?
is there any way to do the second step by user except to call
LuaState.close()?

Andre Naef

unread,
Apr 4, 2012, 8:21:33 PM4/4/12
to jnlua-...@googlegroups.com
For the second step, please see the following:
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/System.html#gc()

Closing the Lua state is not necessary as the Lua state has relinquished its
references to the Java objects already. It now really is up to the Java
garbage collector to do its job.

Reply all
Reply to author
Forward
0 new messages