如何增加内存以处理超大Lua表 [英] How to increase memory to handle super large Lua tables

查看:323
本文介绍了如何增加内存以处理超大Lua表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Lua函数,给定n,它会生成从1到n的序列的所有排列,并将每个唯一序列以表的形式存储在容器表中.

I have a Lua function that, given n, generates all permutations of the series from 1 to n and stores each unique series in table form within a container table.

此生成表的大小很快变得非常大(并且一定如此).大约在我尝试n = 11时,该脚本将运行几秒钟,然后失败以"lua:没有足够的内存".我有16gb的物理RAM,但是通过在Windows任务管理器中观察性能监视器,可以观察到运行时内存的消耗,并且在脚本因内存错误而结束之前,RAM仅上升了约20%.

The size of this generated table gets very large very quickly (and necessarily so). About the time I try n = 11, the script will run for several seconds before failing out to "lua: not enough memory." I have 16gb of physical RAM, but watching the performance monitor in the Windows task manager allows me to watch the ram be consumed during run time, and it only gets up to about 20% before the script ends with the memory error.

我发现这篇帖子看起来像是我要前进的方向: Lua中进程的内存

I found this post that looks like the direction I need to head: memory of a process in Lua

由于我正在使用Lua.exe运行脚本,因此我假设我仅限于Windows为Lua.exe分配的内存量.我可以增加这个数额吗?我可以使用C#包装程序简单地运行Lua脚本(想法是它会具有更高/更少的受限内存分配)吗?还是我看错了方向?

Since I'm running my script with Lua.exe, I'm assuming that I'm limited to how much memory Windows allocates for Lua.exe. Can I increase this amount? Can I use a C# wrapper program to simply run the Lua script (the idea being that it will have a higher/less restricted memory allocation)? Or am I looking in the wrong direction?

推荐答案

是否需要预先存储所有排列?您可以即时生成它们.

Do you need to store all the permutations in advance? You could generate them on-the-fly instead.

示例:

local function genPerm(self, i)
  local result = {}
  local f = 1
  for j = 1, self.n do
    f = f * j
    table.insert(result, j)
  end
  for j = 1, self.n-1 do
    f = f / (self.n + 1 - j)
    local k = math.floor((i - 1) / f)
    table.insert(result, j, table.remove(result, j+k))
    i = i - k * f
  end
  return result
end

local function perms(n)
  return setmetatable({n=n}, {__index=genPerm})
end

local generator = perms(11)
for _, i in ipairs {1, 42, 1000000, 39916800} do
  print(table.concat(generator[i], ','))
end

这篇关于如何增加内存以处理超大Lua表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆