lua-users home
lua-l archive

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

clive wrote:
> I just want to make sure that now LuaJit 2 whether surport more
> than 2GB memory allocation on FreeBSD/x64 or not.

Rest assured, you do NOT want to allocate more than that amount of
Lua objects. The garbage collector in LuaJIT 2.0 (which is the
same as Lua 5.1) is simply not up to the task. Your program would
most certainly slow to a crawl with that many live objects.

But you can allocate huge arrays of data using the LuaJIT FFI via
malloc(), mmap(), VirtualAlloc(), etc. These data structures are
more compact and accessing them is faster, too.

Here's a simple script that allocates and frees an array of
doubles with 2^32 elements, i.e. 32 GB of memory:

local ffi = require("ffi")

void *malloc(size_t size);
void free(void *ptr);

local N = 2^32
local arr = ffi.cast("double *", ffi.C.malloc(N*ffi.sizeof("double")))
assert(arr ~= nil, "out of memory")

print("array =", arr)
arr[0] = 1.5
arr[N-1] = 2.5
print("arr[0] =", arr[0])
print("arr["..(N-1).."] =", arr[N-1])

[Of course, you need to run this with LuaJIT/x64 and have enough
physical memory available.]