PHP致命错误:允许的内存大小为134217728字节已用尽

Posted

技术标签:

【中文标题】PHP致命错误:允许的内存大小为134217728字节已用尽【英文标题】:PHP Fatal error: Allowed memory size of 134217728 bytes exhausted 【发布时间】:2015-07-06 10:10:24 【问题描述】:

您好,我有一个错误提示“致命错误:允许的内存大小为 134217728 字节已用尽(尝试分配 40 字节)”。

基本上,在 php 文件中,我提取了用户选择使用我在 ORACLE 数据库的包中创建的函数进行搜索的信息。在php中我调用函数,传入参数并显示所有结果。

<?php  
$db_connection = oci_connect('DBadmin', 'dbadmin', 'PETLOVERSDB');
if (!$db_connection) 
    $e = oci_error();
    trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
   

$type =  $_GET['type'];  
echo $type;

$result;
$resultArray;
$finalResult = ' ';

$sqlVariableFindMyPets = 'BEGIN :pet_variable := pet_search_package.pet_search_type(:p_type_data);END;';   
$result = oci_new_cursor($db_connection);     

$dataToReceive = oci_parse($db_connection, $sqlVariableFindMyPets);      

oci_bind_by_name($dataToReceive, ':pet_variable', $result, -1, OCI_B_CURSOR);  
oci_bind_by_name($dataToReceive, ':p_type_data', $type); 
oci_execute($dataToReceive); 
oci_execute($result, OCI_DEFAULT); 
oci_fetch_all($result, $resultArray, null, null, OCI_FETCHSTATEMENT_BY_ROW);


oci_close($db_connection);  

if($resultArray == null)
    echo "<h2>No results found<h2>";
 else  
    foreach($resultArray as $iterator)
        $finalResult = $finalResult.'<div class="col-lg-4 col-sm-6"> 
                                        <div class="properties">
                                            <form action="pet-detail.php" method="POST">
                                                <h4>'.$iterator['PET_TYPE_NAME'].' </h4>
                                                <div class="image-holder"><img src="images/logo2.png" class="img-responsive" /></div>
                                                <h5>'.$iterator['PET_RACE_NAME'].' </h5> 
                                                <h5>'.$iterator['PET_COLOR'].' </h5>  
                                                <h5>'.$iterator['PET_ENERGY_LEVEL'].'</h5>
                                                <h5>'.$iterator['PET_COND_NAME'].'</h5> 
                                                <input class="form-control" type="text" style="display: none" readonly name="pet_code" value="'.$iterator['PET_CODE'].'"/>  
                                                <input class="form-control" type="text" style="display: none" readonly name="pet_name" value="'.$iterator['PET_NAME'].'"/> 
                                                <input class="form-control" type="text" style="display: none" readonly name="pet_type" value="'.$iterator['PET_TYPE_NAME'].'"/> 
                                                <input class="form-control" type="text" style="display: none" readonly name="pet_race" value="'.$iterator['PET_RACE_NAME'].'"/> 
                                                <input class="form-control" type="text" style="display: none" readonly name="pet_cond" value="'.$iterator['PET_COND_NAME'].'"/> 
                                                <input class="form-control" type="text" style="display: none" readonly name="pet_energy" value="'.$iterator['PET_ENERGY_LEVEL'].'"/> 
                                                <input class="form-control" type="text" style="display: none" readonly name="pet_learn" value="'.$iterator['PET_LEARN_SKILL'].'"/> 
                                                <input class="form-control" type="text" style="display: none" readonly name="pet_vet" value="'.$iterator['VET_NAME'].'"/> 
                                                <input class="form-control" type="text" style="display: none" readonly name="pet_p_name" value="'.$iterator['PERSON_NAME'].'"/> 
                                                <input class="form-control" type="text" style="display: none" readonly name="pet_location" value="'.$iterator['PETLOCATION'].'"/> 
                                                <input class="form-control" type="text" style="display: none" readonly name="pet_notes" value="'.$iterator['PETNOTES'].'"/> 
                                                <input class="form-control" type="text" style="display: none" readonly name="pet_space" value="'.$iterator['PET_SPACE'].'"/> 
                                                <input class="form-control" type="text" style="display: none" readonly name="pet_treatment" value="'.$iterator['PET_TREATMENT'].'"/> 
                                                <input class="form-control" type="text" style="display: none" readonly name="pet_color" value="'.$iterator['PET_COLOR'].'"/> 
                                                <input class="form-control" type="text" style="display: none" readonly name="pet_sickness" value="'.$iterator['PET_SICKNESS_NAME'].'"/> 
                                                <input class="form-control" type="text" style="display: none" readonly name="pet_med" value="'.$iterator['PET_MED_NAME'].'"/>   
                                                <input type="submit" class="btn btn-primary" value="View Details" />
                                            </form>
                                        </div>
                                       </div>'; 
    
    echo $finalResult;


?>

而ORACLE中的功能是

CREATE OR REPLACE PACKAGE BODY pet_search_package AS
       FUNCTION pet_search_type(TYPEPET in VARCHAR2)
       RETURN SYS_REFCURSOR
       IS 
          pet_search_result SYS_REFCURSOR; 
          type_id NUMBER;

       BEGIN  

       select TP.pet_type_code into type_id
       from dbadmin.petType TP
       where TP.pet_type_name = TYPEPET;

    OPEN pet_search_result FOR select pet_type_name, pet_race_name, pet_cond_name, pet_energy_level, pet_learn_skill, vet_name, person_name, petlocation, petnotes, petabandondescription, pet_space, pet_treatment, pet_color, pet_sickness_name, pet_med_name
    from pet, pettype, petrace, petCondition, petSize,petEnergy, petlearningskill, veterinary, person, petSpace, pettreatments, petcolor, petsickness, petmedicine
    WHERE pettype.pet_type_code = type_id;
        RETURN pet_search_result; 

      EXCEPTION 
        WHEN NO_DATA_FOUND THEN 
          RETURN null;
      END; 

END pet_search_package;

我尝试将ini_set('memory_limit', '-1'); 放在php 文件中,但它似乎不起作用。我还读到我必须增加变量?哪些变量在哪里?我能做些什么来解决这个问题?我不确定发生了什么。请,任何建议将不胜感激!

PS。我在数据库上尝试了该功能,效果很好

【问题讨论】:

更新 php.ini 文件后,您需要重新启动 Web 服务器以使其生效。我不确定 Windows/Mac,但在 Linux 上,您将拥有两个 php.ini 文件,一个用于 cli,一个用于您的 Web 服务器。 @Chad:128 MB 内存绰绰有余。算法应该改进。猜猜如果该脚本同时由 100 个用户运行会发生什么?想买主机?主要问题是脚本使用了太多内存,因为它一次读取所有内容,而不是一次处理每个位。请参阅下面的答案,以及下面我的评论... @Michael 当然。但我没有提供答案,只是发表评论。 【参考方案1】:

我的猜测是你有一个大表,你的程序在准备输出时失败了。

而不是这样做:

        foreach($resultArray as $iterator)
            $finalResult = $finalResult.'<div class="col-lg-4 col-sm-6"> 
                                            <div class="properties">
                                                <form action="pet-detail.php" method="POST">
                                                    <h4>'.$iterator['PET_TYPE_NAME'].' </h4>
                                                    <div class="image-holder"><img src="images/logo2.png" class="img-responsive" /></div>
                                                    <h5>'.$iterator['PET_RACE_NAME'].' </h5> 
                                                    <h5>'.$iterator['PET_COLOR'].' </h5>  
                                                    <h5>'.$iterator['PET_ENERGY_LEVEL'].'</h5>
                                                    <h5>'.$iterator['PET_COND_NAME'].'</h5> 
                                                    <input class="form-control" type="text" style="display: none" readonly name="pet_code" value="'.$iterator['PET_CODE'].'"/>  
                                                    <input class="form-control" type="text" style="display: none" readonly name="pet_name" value="'.$iterator['PET_NAME'].'"/> 
                                                    <input class="form-control" type="text" style="display: none" readonly name="pet_type" value="'.$iterator['PET_TYPE_NAME'].'"/> 
                                                    <input class="form-control" type="text" style="display: none" readonly name="pet_race" value="'.$iterator['PET_RACE_NAME'].'"/> 
                                                    <input class="form-control" type="text" style="display: none" readonly name="pet_cond" value="'.$iterator['PET_COND_NAME'].'"/> 
                                                    <input class="form-control" type="text" style="display: none" readonly name="pet_energy" value="'.$iterator['PET_ENERGY_LEVEL'].'"/> 
                                                    <input class="form-control" type="text" style="display: none" readonly name="pet_learn" value="'.$iterator['PET_LEARN_SKILL'].'"/> 
                                                    <input class="form-control" type="text" style="display: none" readonly name="pet_vet" value="'.$iterator['VET_NAME'].'"/> 
                                                    <input class="form-control" type="text" style="display: none" readonly name="pet_p_name" value="'.$iterator['PERSON_NAME'].'"/> 
                                                    <input class="form-control" type="text" style="display: none" readonly name="pet_location" value="'.$iterator['PETLOCATION'].'"/> 
                                                    <input class="form-control" type="text" style="display: none" readonly name="pet_notes" value="'.$iterator['PETNOTES'].'"/> 
                                                    <input class="form-control" type="text" style="display: none" readonly name="pet_space" value="'.$iterator['PET_SPACE'].'"/> 
                                                    <input class="form-control" type="text" style="display: none" readonly name="pet_treatment" value="'.$iterator['PET_TREATMENT'].'"/> 
                                                    <input class="form-control" type="text" style="display: none" readonly name="pet_color" value="'.$iterator['PET_COLOR'].'"/> 
                                                    <input class="form-control" type="text" style="display: none" readonly name="pet_sickness" value="'.$iterator['PET_SICKNESS_NAME'].'"/> 
                                                    <input class="form-control" type="text" style="display: none" readonly name="pet_med" value="'.$iterator['PET_MED_NAME'].'"/>   
                                                    <input type="submit" class="btn btn-primary" value="View Details" />
                                                </form>
                                            </div>
                                           </div>'; 
        
        echo $finalResult;

试试这个 foreach 循环:

        foreach($resultArray as $iterator)
            echo '<div class="col-lg-4 col-sm-6"> 
                                            <div class="properties">
                                                <form action="pet-detail.php" method="POST">
                                                    <h4>'.$iterator['PET_TYPE_NAME'].' </h4>
                                                    <div class="image-holder"><img src="images/logo2.png" class="img-responsive" /></div>
                                                    <h5>'.$iterator['PET_RACE_NAME'].' </h5> 
                                                    <h5>'.$iterator['PET_COLOR'].' </h5>  
                                                    <h5>'.$iterator['PET_ENERGY_LEVEL'].'</h5>
                                                    <h5>'.$iterator['PET_COND_NAME'].'</h5> 
                                                    <input class="form-control" type="text" style="display: none" readonly name="pet_code" value="'.$iterator['PET_CODE'].'"/>  
                                                    <input class="form-control" type="text" style="display: none" readonly name="pet_name" value="'.$iterator['PET_NAME'].'"/> 
                                                    <input class="form-control" type="text" style="display: none" readonly name="pet_type" value="'.$iterator['PET_TYPE_NAME'].'"/> 
                                                    <input class="form-control" type="text" style="display: none" readonly name="pet_race" value="'.$iterator['PET_RACE_NAME'].'"/> 
                                                    <input class="form-control" type="text" style="display: none" readonly name="pet_cond" value="'.$iterator['PET_COND_NAME'].'"/> 
                                                    <input class="form-control" type="text" style="display: none" readonly name="pet_energy" value="'.$iterator['PET_ENERGY_LEVEL'].'"/> 
                                                    <input class="form-control" type="text" style="display: none" readonly name="pet_learn" value="'.$iterator['PET_LEARN_SKILL'].'"/> 
                                                    <input class="form-control" type="text" style="display: none" readonly name="pet_vet" value="'.$iterator['VET_NAME'].'"/> 
                                                    <input class="form-control" type="text" style="display: none" readonly name="pet_p_name" value="'.$iterator['PERSON_NAME'].'"/> 
                                                    <input class="form-control" type="text" style="display: none" readonly name="pet_location" value="'.$iterator['PETLOCATION'].'"/> 
                                                    <input class="form-control" type="text" style="display: none" readonly name="pet_notes" value="'.$iterator['PETNOTES'].'"/> 
                                                    <input class="form-control" type="text" style="display: none" readonly name="pet_space" value="'.$iterator['PET_SPACE'].'"/> 
                                                    <input class="form-control" type="text" style="display: none" readonly name="pet_treatment" value="'.$iterator['PET_TREATMENT'].'"/> 
                                                    <input class="form-control" type="text" style="display: none" readonly name="pet_color" value="'.$iterator['PET_COLOR'].'"/> 
                                                    <input class="form-control" type="text" style="display: none" readonly name="pet_sickness" value="'.$iterator['PET_SICKNESS_NAME'].'"/> 
                                                    <input class="form-control" type="text" style="display: none" readonly name="pet_med" value="'.$iterator['PET_MED_NAME'].'"/>   
                                                    <input type="submit" class="btn btn-primary" value="View Details" />
                                                </form>
                                            </div>
                                           </div>'; 
        

因此,只需在您遍历它们时显示结果。您的代码将更有效地使用内存。

【讨论】:

不要忘记原帖中的这一行:oci_fetch_all($result, $resultArray, null, null, OCI_FETCHSTATEMENT_BY_ROW); 这个问题同样大.. 我到底应该如何处理 oci_fetch_all($result, $resultArray, null, null, OCI_FETCHSTATEMENT_BY_ROW); ? @Michael 所以,你认为问题不在于内存,而在于实现? @MMYW: 也许有像 oci_fetch_row... 之类的函数?这样您就可以一次读取每一行并打印出来。在伪代码中:while($row = oci_fetch_row(...)) print_row($row); 还有一件事.. 你需要禁用输出缓冲,否则它仍然会变得非常大。无论如何..结果集有多大?是 10000 行还是更多?我只是问,因为我想了解为什么脚本要消耗 >128MB RAM.. @MMYW:不用担心输出缓冲。我对此不是 100% 确定的。我认为如果您自己不调用 ob_start(),它会在缓冲区中有 4KB 时立即打印(甚至可能更早)

以上是关于PHP致命错误:允许的内存大小为134217728字节已用尽的主要内容,如果未能解决你的问题,请参考以下文章

第二个laravel项目(v5.4)PHP致命错误:允许的内存大小为134217728字节已用尽(试图分配262144字节)

致命错误:PHPExcel 中已用尽的允许内存大小为 134217728 字节

使用 pear :致命错误:允许的内存大小为 134217728 字节已用尽(尝试分配 6144 字节)

允许的内存大小 134217728 字节用尽(尝试分配 4294967296 字节)

超大表导致允许的内存大小为 x 字节耗尽错误

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