Created
January 1, 2013 07:45
-
-
Save AndrewTsao/4425791 to your computer and use it in GitHub Desktop.
Measuring lua vm memory usage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$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