Home Backend Development PHP Tutorial PHP unset destroys variables and releases memory

PHP unset destroys variables and releases memory

Aug 08, 2016 am 09:21 AM
echo get memory unset usage

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:

  1. $s=str_repeat('1',255); //Generate a string consisting of 255 ones
  2. $m=memory_get_usage(); //Get the currently occupied memory
  3. unset($s);
  4. $mm=memory_get_usage (); //unset() and then check the currently occupied memory
  5. echo$m-$mm;
  6. ?>

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:

  1. $s=str_repeat('1',256); //Generate a string consisting of 256 ones
  2. $m=memory_get_usage(); //Get the currently occupied memory
  3. unset($s);
  4. $mm=memory_get_usage (); //unset() and then check the currently occupied memory
  5. echo$m-$mm;
  6. ?>

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:

  1. $s=str_repeat('1',256); //This is exactly the same as the second example
  2. $p=&$s;
  3. $m=memory_get_usage();
  4. unset($s); //Destroy $s
  5. $mm=memory_get_usage();
  6. echo$p.'
    ';
  7. echo$m-$mm ;
  8. ?>

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:

  1. $s=str_repeat('1',256); //This is exactly the same as the second example
  2. $p=&$s;
  3. $m=memory_get_usage();
  4. $s=null; //Set $ s is null
  5. $mm=memory_get_usage();
  6. echo$p.'
    ';
  7. echo$m-$mm ;
  8. ?>

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:

  1. $s=str_repeat('1',256); //This is exactly the same as the second example
  2. $p=&$s;
  3. $m=memory_get_usage();
  4. unset($s); //Destroy $s
  5. unset($p);
  6. $mm=memory_get_usage();
  7. echo$p.'
    ';
  8. echo $ m-$mm;
  9. ?>

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.

Statement of this Website
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to automate tasks using PowerShell How to automate tasks using PowerShell Feb 20, 2024 pm 01:51 PM

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

CAMM2 for desktop PCs: MSI explains the benefits of the new RAM standard for gaming towers CAMM2 for desktop PCs: MSI explains the benefits of the new RAM standard for gaming towers Aug 17, 2024 pm 06:47 PM

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

Five selected Go language open source projects to take you to explore the technology world Five selected Go language open source projects to take you to explore the technology world Jan 30, 2024 am 09:08 AM

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 Go language development essentials: 5 popular framework recommendations Mar 24, 2024 pm 01:15 PM

"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

How does java initiate an http request and call the post and get interfaces? How does java initiate an http request and call the post and get interfaces? May 16, 2023 pm 07:53 PM

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

Implementing distributed task scheduling using Golang's web framework Echo framework Implementing distributed task scheduling using Golang's web framework Echo framework Jun 24, 2023 am 11:49 AM

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 development: How to implement WebSockets communication using Laravel Echo and Pusher? Laravel development: How to implement WebSockets communication using Laravel Echo and Pusher? Jun 13, 2023 pm 05:01 PM

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

Example of Curl Get command Example of Curl Get command Mar 20, 2024 pm 06:56 PM

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

See all articles