PHP内存泄漏和派生 [英] PHP memory leak and fork

查看:78
本文介绍了PHP内存泄漏和派生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试避免PHP中的内存泄漏.当我创建一个对象并在最后将其取消设置时,它仍在内存中.未设置的样子:

I'm trying to avoid the memory leak in PHP. When I create a object and unset it at the end it's still in memory. The unset looks like:

$obj = NULL;
unset($obj);

但这仍然无济于事.

我的问题是,当我分叉处理过程并将在子线程中创建和销毁对象时,会发生什么?这会一样吗? 还是有其他方法可以释放内存?

My question is what will happen when I fork the proccess and the object will be created and destructed in the child thread? Will this be the same? Or is there any other way how the free the memory?

这是导入脚本,将消耗少量演出.

This is the import script which will consume few gigs of ram.

推荐答案

PHP 5.3具有可以收集循环引用的垃圾收集器.可能值得尝试:

PHP 5.3 has a garbage collector that can collect cyclic references. It might be worth it to try:

gc_enable();

class A {
  public function __construct() {
    $this->data = str_repeat("A", 1024000);
  }
}

$mem = memory_get_usage();
$a = new A();
$mem2 = memory_get_usage();
$a->a = $a;
$a->a->mydata =  $a->data . 'test';
$mem3 = memory_get_usage();
unset($a);
gc_collect_cycles();
$mem4 = memory_get_usage();      

printf("MEM 1 at start %0.2f Mb\n", ($mem / 1024) / 1024);
printf("MEM 2 after first instantiation %0.2f Mb\n", ($mem2 / 1024) / 1024);
printf("MEM 3 after self-ref: %0.2f Mb\n", ($mem3 / 1024) / 1024);
printf("MEM 4 after unset(\$a): %0.2f Mb\n", ($mem4 / 1024) / 1024);      

输出:

MEM 1 at start: 0.31 Mb
MEM 2 after first instantiation: 1.29 Mb
MEM 3 after self-ref: 2.26 Mb
MEM 4 after unset($a): 0.31 Mb   

这篇关于PHP内存泄漏和派生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆