DEV Community

Cover image for Analyze Lua Memory Leak With Systemtap
jinhua luo
jinhua luo

Posted on

Analyze Lua Memory Leak With Systemtap

The memory used by lua codes is managed by the GC, not calling malloc/free directly. The luajit GC uses mark-and-sweep algorithm. In simple words, it will links all gc objects in a global list. When the memory is in pressure, it would trigger recycle procedures. Note that because of single-threading design, the GC is interspersed in the flow of lua codes (either compiled
or interpretered), e.g. after string allocation. GC would try to check if the gc object is still in used, e.g. as an upvalue, reside in a stack. The GC would sweep all dangling gc objects at the end, and the corresponding memory would be freed and returned to C allocator.

In luajit, lj_alloc_malloc and lj_alloc_realloc would allocate gc objects, and lj_alloc_free would free them. So naturally, we could trace the invocations of these functions to check memory leak.

Note that triggering gc is a bit uncertain and the gc steps would be splited in different time slices. So it would not free unused memory immediately. Moreover, the lua codes may use caches. So it's necessary to check the codes to distinguish the real memory leak.

Please check my blog site:

http://luajit.io/post/2022/analyze-lua-memory-leak-with-systemtap/

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

If you found this post useful, consider leaving a ❤️ or a nice comment!

Got it