PHP提升性能 unset销毁变量并释放内存
PHP unset销毁变量并释放内存
PHP的unset()函数用来清除、销毁变量,不用的变量,我们可以用unset()将它销毁。但是某些时候,用unset()却无法达到销毁变量占用的内存!我们先看一个例子:
<?php $s=str_repeat('1',255); //产生由255个1组成的字符串 $m=memory_get_usage(); //获取当前占用内存 unset($s); $mm=memory_get_usage(); //unset()后再查看当前占用内存 echo $m-$mm;
最后输出unset()之前占用内存减去unset()之后占用内存,如果是正数,那么说明unset($s)已经将$s从内存中销毁(或者说,unset()之后内存占用减少了),可是我在PHP5和windows平台下,得到的结果是:-48。这是否可以说明,unset($s)并没有起到销毁变量$s所占用内存的作用呢?我们再作下面的例子:
<?php $s=str_repeat('1',256); //产生由256个1组成的字符串 $m=memory_get_usage(); //获取当前占用内存 unset($s); $mm=memory_get_usage(); //unset()后再查看当前占用内存 echo $m-$mm;
这个例子,和上面的例子几乎相同,唯一的不同是,$s由256个1组成,即比第一个例子多了一个1,得到结果是:224。这是否可以说明,unset($s)已经将$s所占用的内存销毁了?
通过上面两个例子,我们可以得出以下结论:结论一、unset()函数只能在变量值占用内存空间超过256字节时才会释放内存空间。
那么是不是只要变量值超过256,使用unset就可以释放内存空间呢?我们再通过一个例子来测试一下:
<?php $s=str_repeat('1',256); //这和第二个例子完全相同 $p=&$s; $m=memory_get_usage(); unset($s); //销毁$s $mm=memory_get_usage(); echo $p.'<br />'; echo $m-$mm;
刷新页面,我们看到第一行有256个1,第二行是-48,按理说我们已经销毁了$s,而$p只是引用$s的变量,应该是没有内容了,另外,unset($s)后内存占用却比unset()前增加了!现在我们再做以下的例子:
<?php $s=str_repeat('1',256); //这和第二个例子完全相同 $p=&$s; $m=memory_get_usage(); $s=null; //设置$s为null $mm=memory_get_usage(); echo $p.'<br />'; echo $m-$mm;
现在刷新页面,我们看到,输出$p已经是没有内容了,unset()前后内存占用量之差是224,即已经清除了变量占用的内存。本例中的$s=null也可以换成unset(),如下:
<?php $s=str_repeat('1',256); //这和第二个例子完全相同 $p=&$s; $m=memory_get_usage(); unset($s); //销毁$s unset($p); $mm=memory_get_usage(); echo $p.'<br />'; echo $m-$mm;
我们将$s和$p都使用unset()销毁,这时再看内存占用量之差也是224,说明这样也可以释放内存。那么,我们可以得到另外一条结论:结论二、只有当指向该变量的所有变量(如引用变量)都被销毁后,才会释放内存。
相信经过本文的例子后,大家应该对unset()有所了解了,最起码,本人用unset()也是为了在变量不起作用时,释放内存。

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
