数组中超过64万个元素-内存问题[Dijkstra] [英] More than 640 000 elements in the array - memory problem [Dijkstra]

查看:95
本文介绍了数组中超过64万个元素-内存问题[Dijkstra]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,该脚本将803 * 803 (644 809)图放在其中,每个图中包含1000000的值.使用〜500 * 500,一切正常,但现在崩溃了,它试图分配超过64MB的内存(我还没有).有什么解决方案?莫名其妙地拆分"它还是...?

I have a script which puts 803*803 (644 809) graph with 1 000 000 value inside each. With ~500*500 everything works fine - but now it crashes - it tries to allocate more than 64MB of memory (which I haven't). What's the solution? Somehow "split" it or...?

$result=mysql_query("SELECT * FROM some_table", $connection);
confirm($result);
while($rows = mysql_fetch_array($result)){
    $result2=mysql_query("SELECT * FROM some_table", $connection);
    confirm($result2);
    while($rows2 = mysql_fetch_array($result2)){
        $first = $rows["something"];
        $second = $rows2["something2"];

        $graph[$first][$second] = 1000000;
    }
}

*关于Dijkstra算法

*it's about Dijkstra algorithm

p.s.不,我不能分配超过64MB

p.s. no, I can't allocate more than 64MB

推荐答案

尝试在每个循环结束时使用mysql_free_result($result2);释放内部sql结果,根据PHP版本的不同,PHP脚本可能无法为您完成此操作(由于PHP版本太旧,垃圾收集器可能未启用或可能无用).

Try freeing your inside sql result at the end of each loop, using mysql_free_result($result2);, the PHP script may not do it for you, depending on the PHP version (the garbage collector may not be enabled or may be useless due to a too old PHP version).

不要实例化循环中的两个临时变量,直接使用mysql_fetch_array结果(例如$graph[$rows["something"]][$rows2["something2"]] = 1000000;),每个循环将节省2个内存分配..

Do not instanciate the two temporary variables inside the loop, use the mysql_fetch_array result directly such as $graph[$rows["something"]][$rows2["something2"]] = 1000000; , you will save 2 memory allocations per loop..

PS:这是 micro 优化,因此它可以帮助您节省足够的内存以适合64M的内存.不要忘记,使用64 * 1024 * 1024字节的内存,您的644,809个元素中每个元素的平均最大大小为104字节,加上数组大小本身,以及您可能为算法分配的其余临时数据

PS: This is micro-optimization, therefore it may help you to save enough memory to fit into your 64M of memory. Don't forget that with 64 * 1024 * 1024 bytes of memory, you have an average 104 bytes maximum size for each of your 644 809 elements, plus the array size itself, plus the rest of the temporary data you may allocate for your algorithm.

如果不合适,请考虑拆分矩阵并执行批处理作业等,以较少的内存消耗但运行多个脚本的方式拆分工作.

If it doesn't fit, consider splitting your matrix and doing batched jobs or such to split your work in lesser memory consuming but more than one script run.

这篇关于数组中超过64万个元素-内存问题[Dijkstra]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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