search
Homephp教程php手册PHP垃圾回收机制的理解

php 5.3之前使用的垃圾回收机制是单纯的"引用计数",也就是每个内存对象都分配一个计数器,当内存对象被变量引用时,计数器 1;当变量引用撤掉后,计数器-1;当计数器=0时,表明内存对象没有被使用,该内存对象则进行销毁,垃圾回收完成。

"引用计数"存在问题,就是当两个或多个对象互相引用形成环状后,内存对象的计数器则不会消减为0;这时候,这一组内存对象已经没用了,但是不能回收,从而导致内存泄露;

php5.3开始,使用了新的垃圾回收机制,在引用计数基础上,实现了一种复杂的算法,来检测内存对象中引用环的存在,以避免内存泄露。

该算法可以参考下面这篇文章,这是这篇小总结的主要参考文献,浅谈PHP5中垃圾回收算法(Garbage Collection)的演化.

看下面的例子,代码如下:

<?php  
error_reporting(E_ALL);  
$a = &#39;I am test.&#39;;  
$b = & $a; 
echo $b ."\n";  
//不用说 % php -f gc.php 输出结果非常明了: 
hy0kl% php -f gc.php  
I am test.

好,下一个,代码如下:

<?php  
error_reporting(E_ALL);  
$a = &#39;I am test.&#39;;  
$b = & $a; 
$b = &#39;I will change?&#39;;                                                          
//开源代码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 = &#39;I am test.&#39;;  
$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 = &#39;I am test.&#39;;  
$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 = &#39;I am test.&#39;;  
$b = & $a; 
$a = null; 
echo &#39;$a = &#39;. $a ."\n";  
echo 
&#39;$b = &#39;. $b ."\n";  
猛的第一感觉是什么样的?
hy0kl% php -f gc.php

$a = 

$b =

没错,这就是输出结果,对 PHP GC 已有深入理解的 phper 不会觉得有什么奇怪,说实话,当我第一次运行这段代码时很意外,却让我对 PHP GC 有更深刻的理解了.那么下面与之同工的例子自然好理解了.

Example 6,代码如下:

<?php                                                                         
error_reporting(E_ALL);  
$a = &#39;I am test.&#39;;  
$b = & $a; 
$b = null; 
echo &#39;$a = &#39;. $a ."\n";  
echo &#39;$b = &#39;. $b ."\n";


文章网址:

随意转载^^但请附上教程地址。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version