[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: memory question
- From: Luiz Henrique de Figueiredo <lhf@...>
- Date: Mon, 12 Sep 2016 12:39:22 -0300
> Does collectgarbage('count') report only the memory the Lua core allocated or would it contain memory that a C function allocated using malloc()?
Lua uses a user-supplied function for memory allocation, which may not be
malloc. If you (like most people) create a Lua state using luaL_newstate,
instead of lua_newstate then Lua will use realloc, which shares the same
pool as malloc.
If you want to allocate memory through Lua, call lua_getallocf to get the
current allocator. The protocol for using this allocator is explained in
https://www.lua.org/manual/5.3/manual.html#lua_Alloc
Using lua_newuserdata is equivalent but much more convenient than
calling the allocator manually.
Note that all memory allocated through Lua (either manually or with
lua_newuserdata) is subject to gargabe collection, and so you need to
anchor it somewhere inside the Lua state.