Skip to content

Instantly share code, notes, and snippets.

@jedy
Forked from AndrewTsao/lua_mem_usage.c
Last active October 14, 2015 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jedy/239c08b2f716cf0dd327 to your computer and use it in GitHub Desktop.
Save jedy/239c08b2f716cf0dd327 to your computer and use it in GitHub Desktop.
lua 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment