【问题标题】:How to increase memory to handle super large Lua tables如何增加内存来处理超大 Lua 表
【发布时间】:2012-08-28 17:16:50
【问题描述】:

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

这个生成的表格的大小很快就会变得非常大(而且必然如此)。大约在我尝试 n = 11 的时候,脚本将运行几秒钟,然后出现“lua:内存不足”。我有 16gb 的物理 RAM,但是在 Windows 任务管理器中查看性能监视器可以让我看到在运行时消耗的 ram,并且在脚本以内存错误结束之前它只达到了大约 20%。

我发现这篇文章看起来像是我需要前进的方向:memory of a process in Lua

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


【问题讨论】:

    标签: windows memory-management lua


    【解决方案1】:

    您也许可以在 Lua 的 C++ 端使用memory-mapped file,为此您可以通过LuaBridge 提供 API。

    更新 1:内存映射文件的替代方案可能是 NoSQL 数据库

    【讨论】:

      【解决方案2】:

      与 finn 的回答一样,这里是另一个排列生成器:

      local function perms(a,lo,hi,f)
          if lo>hi then f(a) end
          for i=lo,hi do
              a[lo],a[i]=a[i],a[lo]
              perms(a,lo+1,hi,f)
              a[lo],a[i]=a[i],a[lo]
          end
      end
      
      local function gperms(n,f)
          local a={}
          for i=1,n do a[i]=i end
          perms(a,1,#a,f)
      end
      
      local function show(a)
          for i=1,#a do io.write(a[i],' ') end
          io.write('\n')
      end
      
      gperms(4,show)
      

      【讨论】:

        【解决方案3】:

        您需要提前存储所有排列吗?您可以改为即时生成它们。

        例子:

        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
        

        【讨论】:

          猜你喜欢
          • 2015-07-07
          • 2014-11-06
          • 1970-01-01
          • 2011-06-19
          • 1970-01-01
          • 1970-01-01
          • 2020-02-15
          • 2015-09-13
          • 2021-05-21
          相关资源
          最近更新 更多