Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

你好,这个常驻内存有内存泄漏问题啊 #53

Closed
simayubo opened this issue Nov 8, 2018 · 10 comments
Closed

你好,这个常驻内存有内存泄漏问题啊 #53

simayubo opened this issue Nov 8, 2018 · 10 comments

Comments

@simayubo
Copy link

simayubo commented Nov 8, 2018

No description provided.

@suhanyujie
Copy link

自己在合适的位置,释放一下就行了

@AlexKitC
Copy link

cli下自己释放了自己业务产生的变量,但循环执行一些任务的时候,依然会存在内存一直飙升的问题,看了下源码,没发现是哪儿的问题

@suhanyujie
Copy link

  • 可以贴贴demo,才好分析呀

@AlexKitC
Copy link

AlexKitC commented Dec 3, 2018

for($i=188;$i<1800;$i++){
$tmpp = $i100;
$str = "$tmpp,100";
$res = $db -> query(["
"],"",$str,"");
foreach($res as $v){
$data = [];
$tmp = QueryList::get($v['url'])->encoding('UTF-8','GB2312');
$data['location'] = $tmp->find("#Label18")->text();
$data['num'] = $tmp ->find("#Label16")->text();
$data['date'] = $tmp ->find("#Label24")->text();
$data['price'] = $tmp ->find("#Label23")->text();
$data['company_b'] = $tmp ->find("#Label19")->text();
$data['company_a'] = $tmp ->find("#Label21")->text();
$data['type'] = $tmp ->find("#Label17")->text();
$data['area'] = $tmp ->find("#Label22")->text();
// $db-> update($data,'id='.$v['id']);
echo $v['id']."\r\n";
$tmp = null;
$data = null;
$v = null;
}
$tmpp = null;
$str = null;
$res = null;
echo 'page: '.$i."\r\n";
}

如上:我注释掉所有业务相关代码;
仅保留循环内的:
$tmp = QueryList::get($v['url'])->encoding('UTF-8','GB2312');
和:
$tmp = null;
这两句,打开任务管理器,依然看到cli的php进程没秒2M左右的不停增加,家里的电脑,在350M的时候会触发GC;公司的电脑,会一直+到1.5G内存耗费,然后退出cli,你可以随便找个翻页的网址测试下哈,不加任何业务代码,单纯的QueryList::get,然后unset掉,也没用,现在只能依靠系统的强制GC。

@suhanyujie
Copy link

  • 在本地运行了一下,内存使用确实一直在增长
public function test1()
{
    $gUrl = 'http://www.badmintoncn.com/';
    for($i=188;$i<1800;$i++){
        $tmpp = ($i-1)*100;
        $str = "$tmpp,100";
//            $res = $db -> query([""],"",$str,"");
        $res = [
            'id'=>1,
            'url'=>''
        ];
        for($i=0;$i<100;$i++){
            $data = [];
            $tmp = QueryList::get($gUrl)->encoding('UTF-8','GB2312');
            $data['location'] = $tmp->find("#Label18")->text();
            $data['num'] = $tmp ->find("#Label16")->text();
            $data['date'] = $tmp ->find("#Label24")->text();
            $data['price'] = $tmp ->find("#Label23")->text();
            $data['company_b'] = $tmp ->find("#Label19")->text();
            $data['company_a'] = $tmp ->find("#Label21")->text();
            $data['type'] = $tmp ->find("#Label17")->text();
            $data['area'] = $tmp ->find("#Label22")->text();
            $tmp = null;
            $data = null;
        }
        $size = memory_get_usage();
        echo $this->convert($size).PHP_EOL;
        sleep(1);
        $tmpp = null;
        $str = null;
        $res = null;
        echo 'page: '.$i."\r\n";
    }
}

public function convert($size)
{
    $unit=array('b','kb','mb','gb','tb','pb');
    return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
}
  • 结果部分截图如下:

7.35 mb
page: 100
7.51 mb
page: 100
7.82 mb
page: 100
7.98 mb
page: 100
8.15 mb
page: 100
8.31 mb
page: 100
8.48 mb
page: 100

@AlexKitC
Copy link

AlexKitC commented Dec 3, 2018

把这个问题解决了,搭配其他的任务投递方案,无人值守会更舒服,看好你这个框架哦,加油。持续跟进这个问题。

@simayubo
Copy link
Author

simayubo commented Dec 3, 2018

释放:$query->destruct();
如果是swoole:当work处理到了某个数量的时候重启work进程

@jae-jae
Copy link
Owner

jae-jae commented May 15, 2019

使用完QueryList对象后要及时调用 destruct() 方法释放内存占用。

@slyfalcon
Copy link

全部是动态代理闭包未释放内存。

@jae-jae
Copy link
Owner

jae-jae commented Sep 27, 2020

已优化内存占用

@jae-jae jae-jae closed this as completed Sep 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants