Skip to content

Instantly share code, notes, and snippets.

@AndrewTsao
Created January 1, 2013 07:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AndrewTsao/4425791 to your computer and use it in GitHub Desktop.
Save AndrewTsao/4425791 to your computer and use it in GitHub Desktop.
Measuring lua vm memory usage
#include <stdio.h>
#include <stdlib.h>
#include "lua5.1/lua.h"
#include "lua5.1/lauxlib.h"
#include "lua5.1/lualib.h"
// Measuring lua vm memory usage.
// build: gcc main.c -llua5.1 -lm -ldl -fPIC
static size_t mem_used = 0;
static void *l_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
(void)ud; mem_used -= osize; mem_used += nsize;
if (nsize == 0) {
free(ptr);
return NULL;
} else {
return realloc(ptr, nsize);
}
}
static int panic(lua_State *L) {
printf("PANIC\n");
exit(1);
return 0;
}
int main() {
lua_State *L = lua_newstate(l_alloc, NULL);
if (L) lua_atpanic(L, &panic);
printf("initial memory used: %zu\n", mem_used);
luaL_openlibs(L);
printf("after base libs loaded: %zu\n", mem_used);
luaL_dostring(L, "print(100);");
printf("after print called: %zu\n", mem_used);
lua_close(L);
return 0;
}
@AndrewTsao
Copy link
Author

$uname -a
Linux andi-laptop 3.2.0-23-generic #36-Ubuntu SMP Tue Apr 10 20:41:14 UTC 2012 i686 i686 i386 GNU/Linux

initial memory used: 2371
after base libs loaded: 17059
after print called: 17347

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment