致命错误:我的PHP脚本允许的内存大小(ams)和内存不足(oom)

adhenurcahya28

我总是收到此错误:

Fatal Error: Allowed memory size of X bytes exhausted (tried to allocate X bytes) and also
Fatal Error: Out of memory (allocated X bytes) (tried to allocate X bytes) 

我很困惑,因为这些天(还剩4天)我无法应付。我读了一些关于stackoverflow的建议,但是我仍然遇到同样的问题。

这是我的代码:

<html>
<body>
<?php
$conn = mysqli_connect("localhost","root","","my_trustmovie");
$init = ini_set('memory_limit', '-1');
// global ini_set('memory_limit', '-1');
// ini_get('memory_limit');
// memory_get_usage(true);

//--- FUNCTION **************
function getAllMatrikTrust() {
    global $conn;
    $mt = array();

    $rsrow = mysqli_query($conn,"select * from matrik_trust WHERE connect=1");
    $n = mysqli_num_rows($rsrow);

    if($n>0) {
        while($row = mysqli_fetch_array($rsrow)) {
            $mt[$row['row']][$row['column']] = 1;
        }
        return $mt;
    }
    return array();
}

function getAllInfoJarak() {
    global $conn;
    $mt = array();

    $rsrow = mysqli_query($conn,"select * from info_jarak");
    $n = mysqli_num_rows($rsrow);

    if($n>0) {
        while($row = mysqli_fetch_array($rsrow)) {
            $mt[$row['source']][$row['visited_node']] = $row['jarak'];
        }
        return $mt;
    }
    return array();
}

function getAllMatrikEstimatedTrust() {
    global $conn;
    $mt = array();

    $rsrow = mysqli_query($conn,"select * from matrik_estimatedtrust");
    $n = mysqli_num_rows($rsrow);

    if($n>0) {
        while($row = mysqli_fetch_array($rsrow)) {
            $mt[$row['row']][$row['column']] = $row['value'];
        }
        return $mt;
    }
    return array();
}

function getAllMatrikUserSimilarity() {
    global $conn;
    $mt = array();

    $rsrow = mysqli_query($conn,"select * from matrik_usersimilarity");
    $n = mysqli_num_rows($rsrow);

    if($n>0) {
        while($row = mysqli_fetch_array($rsrow)) {
            $mt[$row['row']][$row['column']] = $row['value'];
        }
        return $mt;
    }
    return array();
}

// ********************* FURTHER

function getMaxLevel($source) {
    global $arrAllInfoJarak;
    $mt = array();
    foreach($arrAllInfoJarak[$source] as $key => $value) {
        array_push($mt,$value);
    }
    return max($mt);
}

function getNodeByLevel($source,$jarak) {
    global $arrAllInfoJarak;
    $mt = array();

    foreach($arrAllInfoJarak as $source_key => $sourceuser) {
        foreach ($sourceuser as $vnode_key => $value){
            if($source_key==$source and $value==$jarak) {
                array_push($mt,$vnode_key);
            }
        }
    }
    return $mt;
}

function getUserSimilarity($row,$column) {
    global $arrAllMatrikUserSimilarity;

    foreach($arrAllMatrikUserSimilarity[$row] as $key => $value) {
        if($key==$column) {
            $sim = $value;
        }
    }

    if($sim) {
        return $sim;
    } else return 0;
}

function getEstimatedTrust($row,$column) {
    global $arrAllMatrikEstimatedTrust;

    foreach($arrAllMatrikEstimatedTrust[$row] as $key => $value) {
        if($key==$column) {
            $et = $value;
        }
    }

    if($et) {
        return $et;
    }  else return 0;
}


function getNodeParent($column) {
    global $arrAllMatrikTrust;
    global $init;
    $mt = array();

    // ini_set('memory_limit', '-1');

    for($i=0;$i<300;$i++) {
        if(isset($arrAllMatrikTrust[$i][$column])) {
            foreach((array)$arrAllMatrikTrust[$i][$column] as $value) { //<<<--------- ERROR >>>
                array_push($mt,$i);     //<<<--------- ERROR >>>
            }
        }
    }   
    return $mt;
}

function calculatePathSim($column) {
    // ini_set('memory_limit', '-1');
    $nodeP= getNodeParent($column);
    $sumPS=0;
    if(count($nodeP)>0) { //ada parent
        foreach($nodeP as $p) {
            $value = getUserSimilarity($p,$column);
            if ($value) {
                if ($value > 0) {
                    $sumPS = $sumPS+($value*calculatePathSim($p));
                }
            }
        }
    } else {
        return 1;
    }
    return $sumPS;
}

function calculateParentTrust($column,$source,$mTV) {
    $nodeP= getNodeParent($column);
    $sumPT = 0; 
    if(count($nodeP)>0) { 
        foreach($nodeP as $p) {
            if($mTV[$source][$p] != 0) {
                $value = getUserSimilarity($p,$column);
                if ($value) {
                    if ($value > 0) {
                        $nodepp = getNodeParent($p); //cek prent2
                        if (count($nodepp) > 0) {
                            foreach($nodepp as $pp) {
                                if(isPathTrust($pp,$source,$mTV)) {
                                    $sumPT = $sumPT + $mTV[$source][$p]; 
                                }
                            }
                        } else {
                            $sumPT = $sumPT + $mTV[$source][$p];
                        }

                    }
                }
            }
        }
    } else {
        return 1;
    }
    return $sumPT;
}

function isPathTrust($column,$source,$mTV) {
    $isTrust = true;
    if ($mTV[$source][$column] == 0) {
        $isTrust = false;
    } else {
        $nodeP= getNodeParent($column);
        if(count($nodeP)>0) { //ada parent
            foreach($nodeP as $p) {
                $isTrust = $isTrust && isPathTrust($p,$source,$mTV);
            }
        }
    }
    return $isTrust;
}

// ===============================================
//                      MAIN 
// ================================================

//Inisialisasi **************
$arrAllMatrikTrust = array();
$arrAllInfoJarak = array();
$arrAllMatrikEstimatedTrust = array();
$arrAllMatrikUserSimilarity = array();

$arrAllMatrikTrust = getAllMatrikTrust();
$arrAllInfoJarak = getAllInfoJarak();
$arrAllMatrikEstimatedTrust = getAllMatrikEstimatedTrust();
$arrAllMatrikUserSimilarity = getAllMatrikUserSimilarity();

//***************************************
$mTV = array();
$uncheck = array();

//00000000000000
   $source= 28;
//00000000000000
$jarak=0;
$maxLevel = getMaxLevel($source);

for($i=0;$i<=$maxLevel;$i++) {
    $nodeL = getNodeByLevel($source,$i);
    foreach($nodeL as $node) {
        $sim = getUserSimilarity($source,$node);
        if ($sim) {
            if ($sim > 0) {
                $sumPS = calculatePathSim($node);
                $sumPT = calculateParentTrust($node,$source,$mTV);
                if ($sumPT > 0) {
                    $mTV[$source][$node] = $sumPS/$sumPT;
                } else {
                    $mTV[$source][$node] = 0;
                }
            } else {
                array_push($uncheck,$node);
                $mTV[$source][$node] = 0;
            }
        } else {
            array_push($uncheck,$node);
            $mTV[$source][$node] = 0; 
        }
    }
}

echo "After trust calculation = <br>";
print_r($mTV); 
echo "<br><br>";

foreach($uncheck as $node) {
    $mTV[$source][$node] = getEstimatedTrust($source,$node);
}

echo "After estimated_trust = <br>";
print_r($mTV); 
echo "<br><br>";


//insert db
foreach($mTV[$source] as $key => $value) {
    mysqli_query($conn,"INSERT INTO trust_value VALUES ('$source','$key','$value')");
}

?>
</body>
</html>

这是我尝试过的一些更改(php.ini在脚本中更改和设置内存)。该错误来自<<<--------- ERROR >>>行(请参见上面的代码)。

When use 128M
134217728 bytes exhausted (tried to allocate 35 bytes) 

256M
Allowed memory size of 268435456 bytes exhausted (tried to allocate 24 bytes) 

512M
Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 35 bytes) 

1024M
Fatal error: Allowed memory size of 1073741824 bytes exhausted (tried to allocate 35 bytes) 

2048M
Fatal error: Out of memory (allocated 1836318720) (tried to allocate 512 bytes

Unlimited
Out of memory (allocated 1836056576) (tried to allocate 35 bytes)

获取数组数据时会出现该错误吗?嗯..那里有90000行。之前我曾将该代码用于少量数据(仅10行),并且运行良好。所以我不知道怎么回事。

发生所有事情是因为我只使用一个变量来获取该数据(在脚本中)吗?我应该在它们上添加或添加什么?

当清楚的时候,我想我calculatePathSim也会函数中得到同样的错误。因为它也使用了getNodeParent函数。以及之后的所有功能。

我的RAM 2GB。这是否意味着我的RAM应该超过2GB?因此,我将RAM更改为4GB,但仍然可以看到它和oom。我是那里的新手,也使用本机PHP。我不知道该怎么办。

我希望我能从大家那里得到建议。需要一些建议。谢谢。

Jacky Cheng

从我看来,您程序的核心输出是

echo "After trust calculation = <br>";
print_r($mTV); 
echo "<br><br>";

foreach($uncheck as $node) {
    $mTV[$source][$node] = getEstimatedTrust($source,$node);
}

echo "After estimated_trust = <br>";
print_r($mTV); 
echo "<br><br>";

//insert db
foreach($mTV[$source] as $key => $value) {
    mysqli_query($conn,"INSERT INTO trust_value VALUES ('$source','$key','$value')");
}

也许对您的代码进行重组是可行的。下面的大多数代码都是无效的,只是概念的演示。

$query_row_count=5000;
while($query_row_count=5000){
    $query_row_count=<get 5000 rows from DB>
    <process them and make your arrays>
    foreach($mTV[$source] as $key => $value) {
        echo "Trust calculation = ".$value.', Estimated trust = ".getEstimatedTrust($value)."<br/>";
        mysqli_query($conn,"INSERT INTO trust_value VALUES ('$source','$key','$value')");
    }
    <not a must, but your could clear those array here xxxarray=null e.t.c.>
}

您可以使用某种计数器来记录您所在的行,以便在再次循环时获得下一个5k行

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Composer require内存不足。PHP致命错误:允许的内存大小为1610612736字节已用尽

Composer require内存不足。PHP致命错误:允许的内存大小为1610612736字节已耗尽旅行者

PHP内存不足更改内存大小无济于事

如何改进我的查询,以免出现“PHP 致命错误:允许的内存大小”(laravel 5.2 中的排序和分页)

PHP图像调整大小致命错误:内存不足

致命错误:opencart中允许的内存大小为67108864

致命错误:XAMPP中已用完允许的内存大小

Symfony缓存:清除php。严重:致命错误:允许的内存大小

PHP 致命错误:允许的内存大小为 2097152 字节已用完(尝试分配 12288 字节)

Symfony 5缓存:清除php。严重:致命错误:允许的内存大小

PHP 致命错误:composer 更新允许的内存大小为 1610612736 字节

如何修复允许的内存大小PHP

允许的内存大小php.ini

PHP致命错误:耗尽了1073741824字节的允许内存大小(尝试分配16777216字节)

致命错误:调整图像大小时,耗尽了允许的内存大小

调整大小和裁剪,内存不足

致命错误:已用尽允许的 268435456 字节内存大小(尝试分配 132120600 字节)

LARAVEL 致命错误异常:已用尽 134217728 字节的允许内存大小(尝试分配 10489856 字节)

致命错误 134217728 字节的允许内存大小在 MySQL 查询表中的总量时耗尽

致命错误:耗尽了536870912字节的允许的内存大小

Zend Framework 1.12 + Crontab + odbc查询-致命错误:允许的内存大小

张量流/ keras模型的内存不足(OOM)错误

PHP内存不足

内存不足PHP

致命错误 - 允许的内存 - PHP

尝试迁移时,PHP致命错误出现内存不足错误

调试“流检查”致命错误:内存不足

Shell脚本内存不足

读取文件时允许的内存大小用尽错误