John Davidson

php - What is consuming too much memory in this script?

0 comments
Message:


I'm having a problem here related to memory being excessively consumed by a function but I'm not able to identify the point at which this could be happening since it is a simple SELECT query and creation of an array with the values for be returned.


The error message is:



Fatal error: Out of memory (allocated 115540492288) (tried to allocate 4096 bytes)



function funGetToken(){

include_once(PATH_DATABASE_INC);
$db = Database::getInstance();
$connection = $db->getConnection();
$id = $_REQUEST['id'];

$arrayMain=[];
$typeArray = [];
$typeSQL = "SELECT * FROM tb_tokentype";
if ($result = $connection->query($typeSQL)) {
while($row = mysqli_fetch_assoc($result)){
$typeArray[$row['id']] = $row['tokenType'];
}
}

array_push($arrayMain,json_encode($typeArray));


$sqlCmd="SELECT tb_tokens.symbol, tb_tokens.name, tb_tokens.idtype, tb_tokens.decimalCases,
tb_tokens.maxSupply, tb_tokens.stake, tb_tokens.delegation, tb_tokens.description, tb_tokens.active, tb_gallery.path,
tb_tokens.idTbGallery
FROM tb_tokens
INNER JOIN tb_gallery
ON tb_tokens.idTbGallery = tb_gallery.id
WHERE tb_tokens.id=".$id;

$viArrayValues1 = [];

if ($result = $connection->query($sqlCmd)) {
$row = mysqli_fetch_assoc($result);
if(!empty($row)){
while($row){
$viArrayValues['id']=$id;
$viArrayValues['symbol']=$row['symbol'];
$viArrayValues['name']=$row['name'];
$viArrayValues['idtype']=$row['idtype'];
$viArrayValues['decimalCases']=$row['decimalCases'];
$viArrayValues['maxSupply']=$row['maxSupply'];
$viArrayValues['stake']=$row['stake'];
$viArrayValues['delegation']=$row['delegation'];
$viArrayValues['active']=$row['active'];
$viArrayValues['description']=$row['description'];
$viArrayValues['path']=$row['path'];
$viArrayValues['idTbGallery']=$row['idTbGallery'];
array_push($arrayMain,json_encode($viArrayValues));
}
} else {
$sqlCmd="SELECT tb_tokens.symbol, tb_tokens.name, tb_tokens.idtype, tb_tokens.decimalCases,
tb_tokens.maxSupply, tb_tokens.stake, tb_tokens.delegation, tb_tokens.description, tb_tokens.active
FROM tb_tokens
WHERE tb_tokens.id=".$id;

if ($result = $connection->query($sqlCmd)) {
while($row = mysqli_fetch_assoc($result)){
$viArrayValues['id']=$id;
$viArrayValues['symbol']=$row['symbol'];
$viArrayValues['name']=$row['name'];
$viArrayValues['idtype']=$row['idtype'];
$viArrayValues['decimalCases']=$row['decimalCases'];
$viArrayValues['maxSupply']=$row['maxSupply'];
$viArrayValues['stake']=$row['stake'];
$viArrayValues['delegation']=$row['delegation'];
$viArrayValues['active']=$row['active'];
$viArrayValues['description']=$row['description'];
$viArrayValues['path']="";
$viArrayValues['idTbGallery']="";
array_push($arrayMain,json_encode($viArrayValues));
}
}
}
}
echo json_encode($arrayMain);
$db->closeConnection();
}

I know I can increase the memory_limit but I still get the error or I get a timout error.
Other than that a simple SELECT and placing it in an array has no reason to need so much memory.


Does anyone have an idea where the problem could be?



My Answer:
The issue in the script that is likely causing excessive memory consumption is the while loop that is not properly fetching rows from the result set. In the following section of the code:

php
if ($result = $connection->query($sqlCmd)) {
$row = mysqli_fetch_assoc($result);
if(!empty($row)){
while($row){
$viArrayValues['id']=$id;
$viArrayValues['symbol']=$row['symbol'];
$viArrayValues['name']=$row['name'];
$viArrayValues['idtype']=$row['idtype'];
$viArrayValues['decimalCases']=$row['decimalCases'];
$viArrayValues['maxSupply']=$row['maxSupply'];
$viArrayValues['stake']=$row['stake'];
$viArrayValues['delegation']=$row['delegation'];
$viArrayValues['active']=$row['active'];
$viArrayValues['description']=$row['description'];
$viArrayValues['path']=$row['path'];
$viArrayValues['idTbGallery']=$row['idTbGallery'];
array_push($arrayMain,json_encode($viArrayValues));
}
}
}


The while loop is not fetching new rows from the result set, causing an infinite loop and consuming excessive memory. To fix this issue, you should replace the while loop with a proper fetch loop like this:

php
if ($result = $connection->query($sqlCmd)) {
while($row = mysqli_fetch_assoc($result)){
$viArrayValues['id']=$id;
$viArrayValues['symbol']=$row['symbol'];
$viArrayValues['name']=$row['name'];
$viArrayValues['idtype']=$row['idtype'];
$viArrayValues['decimalCases']=$row['decimalCases'];
$viArrayValues['maxSupply']=$row['maxSupply'];
$viArrayValues['stake']=$row['stake'];
$viArrayValues['delegation']=$row['delegation'];
$viArrayValues['active']=$row['active'];
$viArrayValues['description']=$row['description'];
$viArrayValues['path']=$row['path'];
$viArrayValues['idTbGallery']=$row['idTbGallery'];
array_push($arrayMain,json_encode($viArrayValues));
}
}


By making this change, the script should properly fetch rows from the result set and avoid the memory consumption issue caused by the infinite loop.

Rate this post

3 of 5 based on 8920 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us