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

致命错误:我的PHP脚本上允许的内存大小(ams)和内存不足(oom),php,memory,memory-management,allocation,Php,Memory,Memory Management,Allocation,我总是会遇到这样的错误: 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的建议,但我还是遇到了同样的问题 这是我的代码: &l

我总是会遇到这样的错误:

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>
我在获取数组数据时是否出现了该错误?嗯..那里有90000排。我以前在处理小数据(只有10行)时使用过该代码,效果非常好。所以我不知道这是怎么回事

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

当这一点清除后,我想我在
calculatePathSim
函数中也会遇到同样的错误。因为它也使用
getNodeParent
函数。以及之后的所有功能

我的内存是2GB。这意味着我的内存应该超过2GB吗?所以,我将RAM改为4GB,但我仍然得到ams和oom。我是这里的新手,也使用原生PHP。我不知道我现在该怎么办


我希望能得到你们所有人的建议。我需要一些建议。谢谢。

这可以通过在脚本顶部添加一行来解决

ini_set("memory_limit","12M");

12M将限制设置为12兆字节(12582912字节)。如果这不起作用,请继续增加内存限制。

据我所见,程序的核心输出是

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')");
}
echo“信任计算后=
”; 印刷(mTV); 回声“

”; foreach($node时取消选中){ $mTV[$source][$node]=getEstimatedTrust($source,$node); } echo“在估计的信任之后=
”; 印刷(mTV); 回声“

”; //插入数据库 foreach($mTV[$source]作为$key=>$value){ mysqli_查询($conn,“插入到信任_值(“$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.>
}
$query\u row\u count=5000;
而($query\u row\u count=5000){
$query\u行\u计数=
foreach($mTV[$source]作为$key=>$value){
回显“信任计算=“.$value.”,估计信任=“.getEstimatedTrust($value)。”
; mysqli_查询($conn,“插入到信任_值(“$source”、“$key”、“$value”)中); } }

你可以用某种计数器来记录你在哪一行,这样当它再次循环时,你就可以得到下一个5k行

甚至在阅读代码之前,你可以给出一个快速的建议。这是因为你在内存中读取和保存了太多的数据,也许你可以尝试像读取5000行->处理前5k行->读取另一个5k行->这样的方法。。。。等等。啊。我希望我能。我不太清楚。嗯,我应该使用变量来保存每个5k数据吗?@jackycheng有什么问题。同样的问题?尝试在php.ini上添加php\u value memory\u limit 128M
$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.>
}