Hello,
it has taken me awhile to realize, but I notice that there is a spike in memory any time a lua file is being accessed, and it never goes down. I have read around that after awhile there are garbage collections, but I have yet to encounter any. I switched between my games two levels so many times it went from 4mb usage to 1gb usage. A level is loaded like so:
int parse_level(const char* filename) {
/* initialize Lua */
L = lua_open();
/* load Lua base libraries */
luaL_openlibs(L);
/* load the Lua file */
if(luaL_loadfile( L, filename) || lua_pcall(L, 0, 1, 0)) {
printf("ERROR: something is wrong with the file!\n");
}
/* parse information */
/* negative indexing means (lua_gettop(L) - n) + 1
push table to -1 */
lua_pushvalue(L, -1);
/* grab the lvl_width and lvl_height */
lvl_width = get_number(L, "width");
lvl_height = get_number(L, "height");
/* grab the lvl_name */
lua_getfield(L, -1, "properties");
lvl_name = get_string(L, "name");
lua_pop(L, 1);
/* before we go to finish parsing, set up the level */
level_back2 = malloc(sizeof(int) * lvl_width * lvl_height);
level_back1 = malloc(sizeof(int) * lvl_width * lvl_height);
level_fore = malloc(sizeof(int) * lvl_width * lvl_height);
/* finish parsing by analyzing the tile layers and objects */
lua_getfield(L, -1, "layers");
/* acquire the tiles placements */
/* BACKGROUND LAYER 2 */
lua_rawgeti(L, -1, 1);
lua_getfield(L, -1, "data");
int i;
for( i = 0; i < lvl_width * lvl_height; i++ ) {
lua_rawgeti(L, -1, i + 1);
level_back2[i] = lua_tonumber(L, -1);
lua_pop(L, 1);
}
lua_pop(L, 2);
/* BACKGROUND LAYER 1 */
lua_rawgeti(L, -1, 2);
lua_getfield(L, -1, "data");
for( i = 0; i < lvl_width * lvl_height; i++ ) {
lua_rawgeti(L, -1, i + 1);
level_back1[i] = lua_tonumber(L, -1);
lua_pop(L, 1);
}
lua_pop(L, 2);
/* FOREGROUND LAYER */
lua_rawgeti(L, -1, 3);
lua_getfield(L, -1, "data");
for( i = 0; i < lvl_width * lvl_height; i++ ) {
lua_rawgeti(L, -1, i + 1);
level_fore[i] = lua_tonumber(L, -1);
lua_pop(L, 1);
}
lua_pop(L, 2);
/* acquire objects */
lua_rawgeti(L, -1, 4);
lua_getfield(L, -1, "objects");
/* get the number of objects in the table */
int t = lua_objlen( L, -1 );
total_objects = t;
/* allocate the number of objects */
alloc_obj( t );
/* process each object */
for( i = 0; i < t; i++ ) {
lua_rawgeti(L, -1, i + 1);
/* print object information to console */
if( print_info == true ) {
printf("--OBJECT--\n");
print_string(L, "name"); print_string(L, "type");
print_number(L, "x"); print_number(L, "y");
print_number(L, "width"); print_number(L, "height");
printf("----------\n");
}
/* get object information */
char *obj_name = get_string(L, "name");
char *obj_type = get_string(L, "type");
int obj_x = get_number(L, "x");
int obj_y = get_number(L, "y");
int obj_w = get_number(L, "width")/2;
int obj_h = get_number(L, "height");
/* create object with information */
create_obj( i, obj_name, obj_type, obj_x, obj_y, obj_w, obj_h );
lua_pop(L, 1);
}
lua_pop(L, 3);
/* close Lua */
lua_close(L);
L = NULL;
}
If you need to know what kind of lua file it reads, make a level in Tiled Map Editor, then go to file -> export as -> Lua. The levels are along the lines of:
return {
properties = {
...
},
tilesets = {
...
},
layers = {
...
}
}
I also have a object script in my game, just one right now, for the player.
Player = {}
function Player:new()
local object = {
...
}
setmetatable(object, { __index = Player })
return object
end
function obj_load()
p = Player:new()
p.ySpeed = p.max_vel
return p
end
function proc()
...
end
When the object is created in the parse_level() function, it opens lua with it's own lua state (for example, every object has an ID, which accesses it's stuff from a struct; so it would be obj[ID].L) and pcalls it, and then
lua_getglobal( obj[ID].L, "obj_load" );
lua_call( obj[ID].L, 0, 1 );
obj[ID].script_loaded = true;
and that's that. In my object function which runs in a loop, it operates the object of the ID it is given, and it sits around doing:
lua_getglobal( obj[ID].L, "proc" );
lua_call( obj[ID].L, 0, 1 );
So you may or may not notice any memory issues that I have, but I am still going to go into more detail. If those last two code lines I posted are not commented out, then the game increases in memory regularly; but if they are commented out, then the game increases in memory only when changing levels (because that's when a lua file is ran, but is closed). So I have pinpointed my memory issue to lua, I just do not know where or how to fix. Any ideas? Thanks
Here is the results.txt I got from Dr.Memory [attachment=22812:drmemory_results_7-26-2014.txt]
EDIT:
I saw this recently: http://stackoverflow.com/questions/9671793/limiting-a-lua-scripts-memory-usage however while the code for the function did compile, I never could figure out how to incorporate it and have the memory limited or that second part of code he posted. So if you tell me that this is what lua does, then is there a way to limit lua's memory usage? I would like to limit it.