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

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
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)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
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

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

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

MySQL storage engine selection comparison: InnoDB, MyISAM and Memory performance index evaluation MySQL storage engine selection comparison: InnoDB, MyISAM and Memory performance index evaluation Jul 26, 2023 am 11:25 AM

MySQL storage engine selection comparison: InnoDB, MyISAM and Memory performance index evaluation Introduction: In the MySQL database, the choice of storage engine plays a vital role in system performance and data integrity. MySQL provides a variety of storage engines, the most commonly used engines include InnoDB, MyISAM and Memory. This article will evaluate the performance indicators of these three storage engines and compare them through code examples. 1. InnoDB engine InnoDB is My

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

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

What are the most popular golang frameworks on the market? What are the most popular golang frameworks on the market? Jun 01, 2024 pm 08:05 PM

The most popular Go frameworks at present are: Gin: lightweight, high-performance web framework, simple and easy to use. Echo: A fast, highly customizable web framework that provides high-performance routing and middleware. GorillaMux: A fast and flexible multiplexer that provides advanced routing configuration options. Fiber: A performance-optimized, high-performance web framework that handles high concurrent requests. Martini: A modular web framework with object-oriented design that provides a rich feature set.

Detailed explanation of the role and usage of the echo keyword in PHP Detailed explanation of the role and usage of the echo keyword in PHP Jun 28, 2023 pm 08:12 PM

Detailed explanation of the role and usage of the echo keyword in PHP PHP is a widely used server-side scripting language, which is widely used in web development. The echo keyword is a method used to output content in PHP. This article will introduce in detail the function and use of the echo keyword. Function: The main function of the echo keyword is to output content to the browser. In web development, we need to dynamically present data to the front-end page. At this time, we can use the echo keyword to output the data to the page. e

See all articles