php 5.3之前使用的垃圾回收机制是单纯的"引用计数",也就是每个内存对象都分配一个计数器,当内存对象被变量引用时,计数器 1;当变量引用撤掉后,计数器-1;当计数器=0时,表明内存对象没有被使用,该内存对象则进行销毁,垃圾回收完成。
"引用计数"存在问题,就是当两个或多个对象互相引用形成环状后,内存对象的计数器则不会消减为0;这时候,这一组内存对象已经没用了,但是不能回收,从而导致内存泄露;
php5.3开始,使用了新的垃圾回收机制,在引用计数基础上,实现了一种复杂的算法,来检测内存对象中引用环的存在,以避免内存泄露。
该算法可以参考下面这篇文章,这是这篇小总结的主要参考文献,浅谈PHP5中垃圾回收算法(Garbage Collection)的演化.
看下面的例子,代码如下:
<?php error_reporting(E_ALL); $a = 'I am test.'; $b = & $a; echo $b ."\n"; //不用说 % php -f gc.php 输出结果非常明了: hy0kl% php -f gc.php I am test.
好,下一个,代码如下:
<?php error_reporting(E_ALL); $a = 'I am test.'; $b = & $a; $b = 'I will change?'; //开源代码phprm.com echo $a ."\n"; echo $b ."\n"; //执行结果依然很明显: hy0kl% php -f gc.php I will change? I will change?
君请看,Example 3,代码如下:
<?php error_reporting(E_ALL); $a = 'I am test.'; $b = & $a; unset($a); echo $a ."\n"; echo $b ."\n"; 是不是得想一下下呢? hy0kl% php -f gc.php Notice: Undefined variable: a in /usr/local/www/apache22/data/test/gc.php on line 8 I am test.
有点犯迷糊了吗?
君再看:Example 4,代码如下:
<?php error_reporting(E_ALL); $a = 'I am test.'; $b = & $a; unset($b); echo $a ."\n"; echo $b ."\n"; 其实如果 Example 3 理解了,这个与之异曲同工. hy0kl% php -f gc.php I am test. Notice: Undefined variable: b in /usr/local/www/apache22/data/test/gc.php on line 9
君且看,Example 5,代码如下:
<?php error_reporting(E_ALL); $a = 'I am test.'; $b = & $a; $a = null; echo '$a = '. $a ."\n"; echo '$b = '. $b ."\n"; 猛的第一感觉是什么样的? hy0kl% php -f gc.php
$a =
$b =
没错,这就是输出结果,对 PHP GC 已有深入理解的 phper 不会觉得有什么奇怪,说实话,当我第一次运行这段代码时很意外,却让我对 PHP GC 有更深刻的理解了.那么下面与之同工的例子自然好理解了.
Example 6,代码如下:
<?php error_reporting(E_ALL); $a = 'I am test.'; $b = & $a; $b = null; echo '$a = '. $a ."\n"; echo '$b = '. $b ."\n";
文章网址:
随意转载^^但请附上教程地址。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Atom editor mac version download
The most popular open source editor
