[luci] Lua memory usage

Conor O'Gorman i at conorogorman.net
Tue Nov 27 17:53:22 CET 2012


Hi,

I'm running the lua intrepreter in the uhttpd process. I have adjusted
the original lua plugin to not fork. This gives a good improvement to
initial page appearance time. It does not change the actual page
production time. It is a trade-off of memory for response time.

But I started seeing the uhttpd memory rising too much. I added a lua
garbage collection call after each request which made an obvious
improvement. 

But there was still a real leak. I looked at the lua heap info and
tracked it to 

+--- a/libs/web/luasrc/dispatcher.lua
++++ b/libs/web/luasrc/dispatcher.lua
+@@ -435,7 +435,7 @@ function dispatch(request)
+ 
+ 	if type(target) == "function" then
+ 		util.copcall(function()
+-			local oldenv = getfenv(target)
++			local oldenv = getfenv()
+ 			local module = require(c.module)
+ 			local env = setmetatable({}, {__index=

'target' in my case, has history. This code would build up a chain of
tables for env. 

I'm still not 100% sure this fix is correct. I don't fully understand
which elements of env are important for the target functions, beyond the
standard environment. My testing has not found a problem, yet.

But, there was still a leak, visible in the uclibc heap, not the lua
memory report. Playing with the uclibc malloc_trim, and also restarting
the lua interpreter did not help. I have resorted to re-starting the
uhttpd process.

I suspect the process memory is getting fragmented, so that a better
allocator is the solution. Lua provides a hook for a custom allocator.

Thoughts?

Thanks,
Conor



More information about the luci mailing list