Lua内存不足 [英] Lua runs out of memory

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

问题描述

我编写了一个复杂的lua脚本,该脚本使用lua套接字库.它从磁盘读取文件列表,按日期对它们进行排序,然后将其发送到HTTP进程.磁盘上的文件数约为65K.taskmanager中的内存使用量不超过200Mb.

I've written a complicated lua script which uses the lua sockets library. It reads a list of files from disk, sorts them by date and sends them to a HTTP process. The number of files on disk is around 65K.The memory usage in taskmanager doesn't exceed 200Mb.

一段时间后,脚本返回:

After quite a while the script returns:

lua: not enough memory

我打印出当前点的GC计数,它永远不会超过110Mb

I print out the current GC count at points and it never goes above 110Mb

local freeMem = collectgarbage('count');
print("GC Count : " .. freeMem/1024 .. " MB");

这是在32位Windows计算机上.

This is on a 32 bit windows machine.

诊断此病的最佳方法是什么?

What's the best way to diagnose this?

推荐答案

所有内存都通过单个lua_Alloc函数.采取以下形式:

All memory goes through the single lua_Alloc function. This takes the form of:

typedef void* (*lua_Alloc) (void* ud, void* ptr, size_t oszie, size_t nsize);

所有分配,重新分配和空闲都通过此过程.可以在此网页中找到此文档.您可以轻松编写自己的文件来跟踪所有内存操作.例如,

All allocations, reallocations and frees go through this. The documentation for this can be found at this web page. You can easily write your own to track all memory operations. For example,

void* MyAlloc (void* ud, void* ptr, size_t osize, size_t nsize)
{
    (void)ud; (void)osize;   // Not used
    if (nsize == 0)
    {
        free(ptr)
        TrackSubtract(osize);
        return NULL;
    }
    else
    {
        void* p = realloc(ptr,nsize);
        TrackSubtract(osize);
        if (p) TrackAdd(nsize);
        return p;
    }
}

您可以将TrackAdd()和TrackSubtract()函数编写为所需的任何内容:调整计数器等等.

You can write the TrackAdd() and TrackSubtract() functions to whatever you want: output to a log; adjust a counter and so on.

要使用新功能,请在创建Lua状态时将指针传递给它:

To use your new function you pass a pointer to it when you create the Lua state:

lua_State* L = lua_newstate(&MyAlloc,0);

此处中找到lua_newstate的文档.

The documentation to lua_newstate is found here.

祝你好运.

这篇关于Lua内存不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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