lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 11/15/06, jason zhang <jzhang@sunrisetelecom.com.cn> wrote:
Thank you so much.
----- Original Message -----
From: "Bennett Todd" <bet@rahul.net>
To: "Lua list" <lua@bazar2.conectiva.com.br>
Sent: Wednesday, November 15, 2006 10:51 PM
Subject: Re: Lua need 1M memory?


I used another method to check how much physical memory (RSS memory in
Linux) will be consumed by an additional Lua state with all standard
libraries loaded.
It turns out that it is just about 20KB (19.6KB in my tests on Linux).

Here is what I did. I took simple Lua stand-alone interpreter from
PiL2 (Listing 24.1, page 219) and augmented it with N dummy Lua
states. N is given as the only command line argument to the program.
The source code of "luai" is attached to this message. A dummy state
is created with luaL_newstate and all the standard libraries are
loaded by means of luaL_openlibs. Then, I ran it with different number
of "dummy" Lua states, let say "luai 0" and "luai 100" and checked RSS
memory usage on Linux in each case. It gave me 19.6KB of resident
physical memory per Lua state.

I think, this is remarkably low overhead. I can create 100 pthreads
with individual Lua states in them at the memory cost of only 2MB.

--Leo--
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

/* The only command line argument is the number of dummy Lua states N to create.  
 * Total number of Lua states in this program is (N+1)
*/


char g_buff[1024]= {0};

int main(int argc, char* argv[])
{
  char prompt[16]= {0};
  int error, i, N;
  lua_State *l= NULL;
  lua_State *L = luaL_newstate();  /* opens Lua */
  luaL_openlibs(L);                /* opens the standard libraries */

  if( argc != 2) 
    { 
      fputs("Usage:\n    sh$ luai N\nwhere N is a number of dummy Lua states\n",
            stderr);
      exit(1);
    }
  N= atoi( argv[1]);
  sprintf(prompt, "L%d> ", N+1);

  for(i=0; i<N; i++)
    {
      l= luaL_newstate();
      luaL_openlibs(l);
    }

  while(1)
    {
      fputs(prompt, stdout);
      fflush( stdout);
      if( fgets(g_buff, sizeof(g_buff), stdin) == NULL){ break;}
      /* fputs(g_buff, stdout); */
      error = luaL_loadbuffer(L, g_buff, strlen(g_buff), "line") ||
        lua_pcall(L, 0, 0, 0);
      if( error){
        fprintf(stderr, "%s", lua_tostring(L, -1));
        lua_pop(L, 1);  /* pop error message from the stack */
      }
    }
  lua_close(L);
  return 0;
}

/*EoF*/