我总是收到此错误:
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。我不知道该怎么办。
我希望我能从大家那里得到建议。需要一些建议。谢谢。
从我看来,您程序的核心输出是
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] 删除。
我来说两句