首页 文章

PHP致命错误允许内存大小耗尽

提问于
浏览
3

我正在编写一个codeigniter应用程序,在执行查询时遇到以下致命错误 .

致命错误:第262行/var/www/html/cryd/_zcore/core/Loader.php中允许的内存大小为134217728字节(尝试分配262144字节)

我可以增加允许的内存大小,但似乎问题可能更严重,如果它是一个内存泄漏,我只是给PHP更多的内存来玩 . 查询甚至不是那么密集,它只返回一行数据 .

这是我的控制器

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Invest_webhook extends CI_Controller {


    private $web_msg = null;
    private $proceed = 1;
    private $data = null;
    private $receive = null;
    private $complete = 0;
    private $member = null;
    private function loadPage($page,$data=null){
        $data = array_merge($data,$this->lang->language);
        $this->parser->parse('dashboard/header',$data);
        $this->parser->parse('dashboard/'.$page);
        $this->parser->parse('dashboard/footer');
    }

    public function webhook(){
        echo memory_get_peak_usage()."
"; //$update = file_get_contents("php://input"); //$update = json_decode($update,true); $update = array( 'notification_id' => '57233292b6a3d133e9c83822', 'delivery_attempt' => 1, 'type' => 'address', 'data' => Array( 'network' => "BTCTEST", 'address' => '2N1jfZt8Uc721FAWNjdVpQZjfTxeG271RKy', 'balance_change' => 1.00000000, 'amount_sent' => 0.00000000, 'amount_received' => 1.00000000, 'txid' => '', 'confirmations' => 4, 'is_green' => false ), 'created_at' => 1497176944 ); $data = $this->get_investment_data($update['data']['address']); if(!$data){ die('This address does not exist'); } $this->data = $data[0]; $this->member = $this->get_member_data($this->data['tid']); //Start the process to verify transaction and credit investment $this->verify_investment(); $this->update_investment(); //$parameter = $this->web_msg; //include APPPATH."libraries/telegrambotv2.php"; //$bot = new Telegram('331263599:AAEpHNAdyN1X5TenBk_QkJdt7xfwzDI6YeQ','bot'); echo $this->web_msg."
"; } private function verify_investment(){ include APPPATH."/libraries/BlockIo.php"; $this->load->config('block'); $block = new BlockIo($this->config->item('api_token'),$this->config->item('api_secret')); $address = ($this->data['address']); $receive = $block->get_address_balance(array('addresses' => $address)); $receive = $receive->data->available_balance; $expect = $this->data['inv']; settype($receive,'float'); settype($expect,'float'); echo '
'.$receive."
".$expect."
"; if($receive == $expect){ $this->receive = $receive; return true; } $this->proceed = 0; $this->web_msg = 'inv_mismatch'; return false; } private function get_member_data($tid){ $this->load->model('invest_model','invest'); return $this->get_member_data($tid); } private function update_investment(){ if(!$this->proceed){ return; } $this->load->model('invest_model','invest'); $einv = $this->member['inv']; settype($einv,'float'); $new_inv = $einv + $this->receive; $this->member['inv'] = $new_inv; $this->member['last_inv'] = 'NOW()'; $this->data['confirmed'] = 1; if($this->invest->update_investment($this->data,$this->member)){ $this->web_msg = 'inv_complete'; $this->complete = 1; return true; } else { $this->proceed = 0; $this->web_msg = 'inv_unx_err'; return false; } } private function get_investment_data($address){ $this->load->model('invest_model','invest'); return $this->invest->investment_data($address); } private function log($text){ $file = fopen(APPPATH."/logs/investments/log.log",'a'); fwrite($file,$text); fwrite($file,"\n"); fclose($file); } }

这是我的模特

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Invest_model extends CI_Model {
    public function __construct(){
        parent::__construct();
    }

    public function investment_data($address){
        $this->db->where('address',$address);
        $this->db->where('confirmed',0);
        $f = $this->db->get('investments');
        return $f->result_array();
    }

    public function current_investment($tid){
        $this->db->select('inv');
        $this->db->where('tid',$tid);
        $f = $this->db->get('users');
        $f = $f->row_array();
        return $f;
    }

    public function get_member_data($tid){
        echo "<hr/>";
        echo memory_get_peak_usage()."
"; $this->db->where('tid',$tid); echo memory_get_peak_usage()."
"; $f = $this->db->get('users'); echo memory_get_peak_usage()."
"; return $f->row_array(); } public function update_investment($inv,$member){ $this->db->trans_begin(); $this->db->set($inv); $this->db->where('address',$inv['address']); $this->db->update('investments'); $this->db->set($member); $this->db->where('tid',$member['tid']); $this->db->update('users'); if($this->db->trans_status() === FALSE){ $this->db->trans_rollback(); return false; } else { $this->db->trans_commit(); return true; } } }

我不是在哪里可以进一步优化它,它非常简单和直接,如果它在我的逻辑appraoch中的问题,希望比我的更好的头脑将能够发现并指导我进一步 .

提前致谢 .

5 回答

  • 0

    把这一行放在你的codeigniter的index.php上 . 所以整个项目都会受到影响 .

    ini_set('memory_limit', '-1');
    

    Note: This is just a quick fix. This bug has not been resolved in php itself.

  • 3

    你的代码看起来很好 . 检查是否重复调用某些函数 .

    你可以用

    ini_set('memory_limit', '-1');
    

    作为临时解决方案 . 或者只是增加 memory_limit

    Chceck你 php.ini 文件 . 如果是以下形式,

    memory_limit=8G
    

    MB 的形式更改

    memory_limit=8192M
    

    您也可以在代码中执行此操作

    ini_set('memory_limit', '8192M');
    
  • 0

    如果你想知道内存的去向,你应该查看PHP内存分析:PHP memory profiling

    就个人而言,我只是将PHP max_memory增加到256MB并查看它是否足够;)

  • 3

    出现此问题是因为私有方法一直反复调用自己,直到分配的内存耗尽并且脚本停止执行

    private function get_member_data($tid){
        return $this->get_member_data($tid);
    }
    

    这是一个愚蠢的人为错误,上面的方法应该重写如下

    private function get_member_data($tid){
    //Call to model method
        return $this->invest->get_member_data($tid);
    }
    

    对不起,谢谢大家 .

  • 0

    如果有其他人有这个问题:

    确保php.ini中的内存大小在数字后面有一个M.

    我在那里有512 ....当我把它改为512M .....没有更多的问题 .

    ž

相关问题