我总是收到这个错误:
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
函数。并且也适用于它之后的所有功能。
我的内存2GB。这是否意味着我的RAM应该超过2GB?所以,我已经将内存更改为4GB,但我仍然可以获得这些内容。我是那里的新手,也使用原生PHP。我不知道我现在该做什么。
我希望得到大家的建议。需要一些建议。感谢。
答案 0 :(得分:0)
这可以通过在脚本顶部添加一行
来解决ini_set("memory_limit","12M");
12M将限制设置为12兆字节(12582912字节)。如果这不起作用,请继续增加内存限制。
答案 1 :(得分: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')");
}
也许您的代码重组会起作用。下面的大部分代码都是无效的,它只是概念的演示。
$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行