PHP unset destroys variables and releases memory
PHP’s unset() function is used to clear and destroy variables. We can use unset() to destroy unused variables. But sometimes, using unset() cannot destroy the memory occupied by the variable! Let’s look at an example first:
- $s=str_repeat('1',255); //Generate a string consisting of 255 ones
- $m=memory_get_usage(); //Get the currently occupied memory
- unset($s);
- $mm=memory_get_usage (); //unset() and then check the currently occupied memory
- echo$m-$mm;
- ?>
The memory occupied before final output unset() minus unset () takes up memory. If it is a positive number, it means that unset($s) has destroyed $s from the memory (or in other words, the memory usage has decreased after unset()). However, under PHP5 and Windows platforms, I got The result is: -48. Does this mean that unset($s) does not destroy the memory occupied by variable $s? Let’s make the following example again:
- $s=str_repeat('1',256); //Generate a string consisting of 256 ones
- $m=memory_get_usage(); //Get the currently occupied memory
- unset($s);
- $mm=memory_get_usage (); //unset() and then check the currently occupied memory
- echo$m-$mm;
- ?>
This example is almost the same as the above example, the only thing The difference is that $s consists of 256 1's, which is one more 1 than the first example, and the result is: 224. Does this mean that unset($s) has destroyed the memory occupied by $s?
Through the above two examples, we can draw the following conclusions: Conclusion 1. The unset() function can only release the memory space when the variable value occupies more than 256 bytes of memory space.
So as long as the variable value exceeds 256, can using unset free up memory space? Let’s test it with another example:
- $s=str_repeat('1',256); //This is exactly the same as the second example
- $p=&$s;
- $m=memory_get_usage();
- unset($s); //Destroy $s
- $mm=memory_get_usage();
- echo$p.'
'; - echo$m-$mm ;
- ?>
Refresh the page, we see that the first line has 256 1s, and the second line is -48. It stands to reason that we have destroyed $s, and $p is just a variable that refers to $s, and there should be no content. In addition, the memory usage after unset($s) has increased compared with before unset()! Now let’s do the following example:
- $s=str_repeat('1',256); //This is exactly the same as the second example
- $p=&$s;
- $m=memory_get_usage();
- $s=null; //Set $ s is null
- $mm=memory_get_usage();
- echo$p.'
'; - echo$m-$mm ;
- ?>
Now refresh the page, we see that the output $p has no content, and the difference in memory usage before and after unset() is 224, that is, the memory occupied by the variable has been cleared. $s=null in this example can also be replaced by unset(), as follows:
- $s=str_repeat('1',256); //This is exactly the same as the second example
- $p=&$s;
- $m=memory_get_usage();
- unset($s); //Destroy $s
- unset($p);
- $mm=memory_get_usage();
- echo$p.'
'; - echo $ m-$mm;
- ?>
We use unset() to destroy both $s and $p. At this time, the difference in memory usage is also 224, indicating that this can also release memory. Then, we can get another conclusion: Conclusion 2. The memory will be released only when all variables (such as reference variables) pointing to the variable are destroyed.
I believe that after the examples in this article, everyone should have an understanding of unset(). At the very least, I use unset() to release memory when the variable does not work.
The above introduces PHP unset to destroy variables and release memory, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

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

If you are an IT administrator or technology expert, you must be aware of the importance of automation. Especially for Windows users, Microsoft PowerShell is one of the best automation tools. Microsoft offers a variety of tools for your automation needs, without the need to install third-party applications. This guide will detail how to leverage PowerShell to automate tasks. What is a PowerShell script? If you have experience using PowerShell, you may have used commands to configure your operating system. A script is a collection of these commands in a .ps1 file. .ps1 files contain scripts executed by PowerShell, such as basic Get-Help

The first LPCAMM2 modules for laptops are already being delivered, and desktop mainboards are also expected to be equipped with CAMM2 in future. CAMM2 and LPCAMM2 are not compatible with each other, and even on desktop PCs, customers need to be caref

In today's era of rapid technological development, programming languages are springing up like mushrooms after a rain. One of the languages that has attracted much attention is the Go language, which is loved by many developers for its simplicity, efficiency, concurrency safety and other features. The Go language is known for its strong ecosystem with many excellent open source projects. This article will introduce five selected Go language open source projects and lead readers to explore the world of Go language open source projects. KubernetesKubernetes is an open source container orchestration engine for automated

"Go Language Development Essentials: 5 Popular Framework Recommendations" As a fast and efficient programming language, Go language is favored by more and more developers. In order to improve development efficiency and optimize code structure, many developers choose to use frameworks to quickly build applications. In the world of Go language, there are many excellent frameworks to choose from. This article will introduce 5 popular Go language frameworks and provide specific code examples to help readers better understand and use these frameworks. 1.GinGin is a lightweight web framework with fast

1. Java calls post interface 1. Use URLConnection or HttpURLConnection that comes with java. There is no need to download other jar packages. Call URLConnection. If the interface response code is modified by the server, the return message cannot be received. It can only be received when the response code is correct. to return publicstaticStringsendPost(Stringurl,Stringparam){OutputStreamWriterout=null;BufferedReaderin=null;StringBuilderresult=newSt

With the development of the Internet and the advancement of information technology, the era of big data has arrived, and fields such as data analysis and machine learning have also been widely used. In these fields, task scheduling is an inevitable problem. How to achieve efficient task scheduling is crucial to improving efficiency. In this article, we will introduce how to use Golang's web framework Echo framework to implement distributed task scheduling. 1. Introduction to the Echo framework Echo is a high-performance, scalable, lightweight GoWeb framework. It is based on HTTP

Laravel is a popular PHP framework that is highly scalable and efficient. It provides many powerful tools and libraries that allow developers to quickly build high-quality web applications. Among them, LaravelEcho and Pusher are two very important tools through which WebSockets communication can be easily implemented. This article will detail how to use these two tools in Laravel applications. What are WebSockets? WebSockets

In Linux, URL or Curl client is a popular command line utility that allows you to transfer data over the network using various protocols such as HTTPS, HTTP, FTP, etc. It allows you to send and receive data using its get, post and request methods. Among them, you need to use the "get" method frequently. Therefore, it becomes crucial to learn various methods and various options that you can use to increase your productivity. "Performing a curl operation is as simple as entering a few simple commands. Although it seems simple, many users do not fully realize its potential. Therefore, this short guide provides some information on how to perform curl operations on Linux systems. Example using the "curlget" command." Curl
