What's new in PHP V5.2, part 1th: Using the new memory manager

Source: Internet
Author: User
Tags mysql functions php windows server hosting ibm developerworks
PHP 5.2 Memory Manager


What's new in PHP V5.2, part 1th: Using the new memory manager

Track and monitor PHP memory like tracking and monitoring uber-nerd

Tracy Peterson ([email protected]), freelance writer, consultant

Since 1997, Tracy Peterson has served as an IT project manager and Web developer, and currently serves as the Microsoft MSN Search Operational Program director. He is currently working in San Francisco.

Introduction:Learn how to use the new memory manager introduced in PHP V5.2 and begin to be proficient in tracking and monitoring memory usage. This will allow you to use more memory more efficiently in PHP V5.2.

See more in this series

Mark this article!

Release date:? April 10, 2007
level:? Intermediate
access situation ? 1145 views
Suggestion:? 0? (Add comment)

Average score (2 ratings total)

PHP V5.2: Getting Started

November 2006 released PHP V5.2, which includes many new features and bug fixes. It abolished version 5.1 and was recommended for all PHP V5 users to upgrade. My favorite lab Environment--windows, Apache, MySQL, PHP (WAMP)-has been introduced into V5.2 's new package (see Resources). Where will you find it in Windows? Install PHP V5.2, MySQL, and Apache applications on XP or 2003 computers. You can install it very easily, it has a lot of good small management benefits, and I highly recommend using it.

While this is the simplest package for Windows users, you need to add the following code when you configure PHP on Linux: --memory-limit-enabled (except for any other options that apply to your server). However, under Windows, a function is provided to resolve this problem.

There are many improvements in PHP V5.2, and a critical area is memory management. From the README. Exactly quoted in ZEND_MM is: The goal of the new memory manager (PHP5.2 and later) is to reduce memory allocation overhead and accelerate memory management. ”

Here are some of the key elements in the V5.2 release notes:

    • Unnecessary configuration options have been removed --disable-zend-memory-manager
    • Added --enable-malloc-mm configuration options, this configuration option is enabled by default when debugging builds to allow internal and external memory debuggers to be used
    • Allow usage ZEND_MM_MEM_TYPE and ZEND_MM_SEG_SIZE environment variables to adjust the memory manager

To understand the implications of these new features, we need to delve into the art of memory management and consider why allocation overhead and speed are big issues.

Back to top of page

Why is memory management?

One of the fastest-growing technologies in computing is memory and data storage, which are driven by continuous demands such as increased speed and storage size. Early computers used cards as memory and then turned to chip technology. Can you imagine working on a computer with only 1 KB RAM memory? Many of the early computer programmers have used them. These pioneers soon realized that to work under technical constraints, they would have to be careful to avoid overloading the system with trivial commands.

As a PHP developer, our environment is more convenient to encode than colleagues who encode in C + + or other stricter languages. In our world, we don't have to worry about how to deal with system memory ourselves, because PHP will handle this problem for us. However, in other programming areas, responsible coders will use various functions to ensure that the executed commands do not overwrite some other program data-thus destroying the program's operation.

Memory management is typically handled by requests from coders to allocate and free memory blocks. An allocation block can hold any type of data, and this process will separate a certain amount of memory for that data and provide an access method for the application when the operation requires access to the data. It is expected that the program frees the allocated memory after any operation has been completed and allows the system and other programmers to use the memory. If the program does not release memory back to the system, it is called a memory leak .

Disclosure is a common problem with any running program and is generally acceptable in some way, especially when we know that the running program terminates immediately and frees all memory allocated to the program by default.

This is a problem, as with almost all client applications, because of the random running and terminating programs. It is expected that the server application will run in an indeterminate manner without terminating or restarting, which makes memory management absolutely essential for server Daemon programming. In a long-running program, even a small leak eventually develops into a system weakness because the memory block is already in use and is never freed.

Back to top of page

Long-term consideration

As with any language, a permanent server daemon written in PHP can be used for a number of purposes. But when we start using PHP for these purposes, we must also consider memory usage.

Scripts that parse large amounts of data or that may hide an infinite loop tend to consume a lot of memory. Obviously, once the memory is exhausted, the performance of the server is reduced, so we must also pay attention to memory usage when executing the script. While we can simply observe the amount of memory used by enabling System Monitor, it does not tell us anything more useful than the overall system memory state. Sometimes we need more than just help with troubleshooting or optimizing content, and sometimes we simply need more details.

One way to get the transparency of the script's content is to use an internal or external debugger. The internal debugger is the same process that is rendered as a script execution. Independent processes that are considered from the perspective of the operating system are external debuggers . Using the debugger for memory analysis is similar to any situation, but uses different methods to access memory. The internal debugger has direct access to the memory space where the running process resides, while the external debugger accesses the memory through the socket.

There are many methods and available debug servers (external) and libraries (internal) that can be used for secondary development. In order to be ready to debug the PHP installation, you can use the new provided --enable-malloc-mm , which DEBUG is enabled by default in the build. This enables environment variables to USE_ZEND_ALLOC be used to allow the selection of malloc or emalloc memory allocations at run time. Using Malloc-type memory allocation will allow the external debugger to observe memory usage, whereas EMALLOC allocations will use the Zend memory Manager abstraction, which requires internal debugging.

Back to top of page

Memory management functions in PHP

In addition to making the memory manager more flexible and transparent, PHP V5.2 also memory_get_usage() memory_get_peak_usage() provides a new parameter, which allows you to see the amount of memory used. The new Boolean value mentioned in the description is real_size . By calling the function memory_get_usage($real); (where $real = true ), the result will be the actual allocated memory size in the system at the time of the call, including the memory manager overhead. If you do not use a tag group, the returned data will include only the memory used within the run script, minus the memory manager overhead.

The difference is that the latter returns the highest amount of memory for the memory_get_usage() memory_get_peak_usage() running process that was called so far, while the former only returns the usage at execution time.

For memory_get_usage() , Php.net provides the code snippet in Listing 1.

Listing 1. memory_get_usage() Example

In this simple example, we first turn the result of a direct call memory_get_usage() , and the code comment shows a common result that might have 36640 bytes in the author's system. Then we use 4,242 "Hello" copies to load $a and run the function again. The output of this simple application can be seen in Figure 1.

Figure 1. memory_get_usage() The sample output

There are no memory_get_peak_usage() examples, because the two are very similar and the syntax is the same. However, for the example code in Listing 1, there will be only one result, that is, the highest memory usage at the time. Let's take a look at Listing 2.

Listing 2. memory_get_peak_usage() Example

The code in Listing 2 is the same as in Figure 1, but memory_get_usage() has been replaced with memory_get_peak_usage() . The $a output will not change much until we populate with 4,242 "Hello" copies. The memory jumps to 57960, indicating the peak so far. When we check the peak memory usage, we get the highest value so far, so all the further calls will be 57960 until we handle more of the processing than $a the memory used (see Figure 2).

Figure 2. memory_get_peak_usage() The sample output

Back to top of page

Limit Memory usage

One way to ensure that the server hosting your application does not load is to limit the amount of memory used by any script that PHP executes. This is not something we should do at all, but since PHP is a loosely typed language and is parsed at runtime, we sometimes get scripts that are poorly written when released into production applications. These scripts may perform loops or open a long list of files, forgetting to close the current file before opening a new file. In either case, a poorly written script might end up consuming a lot of memory before you know it.

In PHP. INI, you can use the configuration parameters memory_limit to specify the maximum amount of memory that any script can run in the system. This is not a specific change for V5.2, but the memory manager and any discussions it uses are worth at least a quick look at this feature. It also carefully guides me through the last few new features of the memory Manager: Environment variables.

Back to top of page

Adjusting the memory manager

Finally, how do you program in situations where you can't be a perfectionist but perfectly fit your goals? The new environment variables ZEND_MM_MEM_TYPE and ZEND_MM_SEG_SIZE exactly meet your needs.

When the memory manager allocates a large block of memory, it is the ZEND_MM_SEG_SIZE scheduled size listed in the install variable to perform the operation. The default partition size for these memory blocks is per block of KB, but you can adjust these partition sizes to meet special needs. For example, if you notice that the operations in one of the most commonly used scripts result in a large amount of memory waste, you can adjust this size to a value that is closer to the matching script requirement, reducing the amount of memory allocated but the remaining amount of memory remains zero. Under the right conditions, such cautious compounding adjustments can make a huge difference.

Back to top of page

Retrieving memory usage in Windows

If you have pre-built PHP Windows binaries and don't use options at build time --enable-memory-limit , you'll need to browse this section before continuing. For Linux, configure PHP with the option to build PHP when building --enable-memory-limit .

To retrieve memory usage using Windows binary code, create the following function.

Listing 3: Getting memory usage under Windows

Save the results to a file named function.php. You can now only include this file in a script that needs to use it.

Back to top of page

Hands-on practice

Let's take a look at the benefits of using the actual examples of these settings. There may be times when you want to know why the memory is not properly allocated at the end of the script. The reason is because some of the functions themselves cause memory leaks, especially if you use only the built-in PHP functions. Here you will find out how to discover such issues. And in order to begin the conquest of memory leak lookups, you will create a test MySQL database, as shown in Listing 4.

Listing 4. Creating a Test database
                Mysql> CREATE DATABASE memory_test;mysql> use memory_test;mysql> CREATE TABLE Leak_test       (ID int. NOT NULL PRI Mary Key auto_increment,         data varchar (255) NOT null default ');mysql> INSERT into leak_test (data) VALUES ("Data1" ), ("Data 2"), ("Data 3"), ("Data 4"), ("Data 5"), ("Data6"), ("Data 7"), ("Data 8"), ("Data 9"), ("Data 10")       ;

This creates a simple table with an ID field and a data field.

In the next list, imagine that our tenacious programmers are executing some MySQL functions, especially using mysql_query() the result to apply to variables. When he does this, he will notice that mysql_free_result() some memory is not freed even when called, causing memory usage to grow as the Apache process continues (see Listing 5).

Listing 5. Memory Leak Detection Example
                for ($x =0; $x <300; $x + +) {    $db = mysql_connect ("localhost", "root", "test");    mysql_select_db ("test");    $sql  = "Select data from Test";     $result = mysql_query ($sql); The operation suspected of leaking    mysql_free_result ($result);    Mysql_close ($DB); }

Listing 5 is a simple MySQL database operation that is likely to be used in any location. When we run the script, we notice some strange behavior related to memory usage and need to check it out. In order to use the memory management function so that we can verify where the error occurred, we will use the following code.

Listing 6. Example of a calibration lookup error
     "; $db = mysql_connect ("localhost", "User", "password"), mysql_select_db ("memory_test"); echo "After connecting, we ' re using (in bytes): ", Memory_get_usage ()," \ n
"; for ($x =0; $x <10; $x + +) {$sql = "Select data from Leak_test WHERE id= '". $x. "'"; $result = mysql_query ($sql); The operation//suspected of leaking. echo "After query # $x, we ' re using (in bytes):", Memory_get_usage (), "\ n
"; Mysql_free_result ($result); echo "After freeing result $x, we ' re using (in bytes):", Memory_get_usage (), "\ n
";} Mysql_close ($DB); echo "After closing the connection, we ' re using (in bytes):", Memory_get_usage (), "\ n
"echo" Peak memory usage for the script (in bytes): ". Memory_get_peak_usage ();? >

Note: Check the current memory usage by the defined interval. In the output below, by showing that our script has been allocating memory for the function and not freeing the memory at the time it should be freed, to provide an actual test of the memory leak, you can see how the memory usage grows with each call.

Listing 7. Test Script output
                At the start of the we ' re using (in bytes): 63216After connecting, we ' re using with (in bytes): 64436After query #0, W E ' re using (in bytes): 64760After Freeing result 0, we ' re using with (in bytes): 64828After query #1, we ' re using with (in bytes): 6  5004After freeing result 1, we ' re using (in bytes): 65080After query #2, we ' re using (in bytes): 65160After freeing result 2, we ' re using (in bytes): 65204After query #3, we ' re using (in bytes): 65284After freeing result 3, the we ' re using (in byte  s): 65328After query #4, we ' re using (in bytes): 65408After Freeing result 4, we ' re using with (in bytes): 65452After query #5,  We ' re using (in bytes): 65532After Freeing result 5, the we ' re using (in bytes): 65576After Query #6, the we ' re using (in bytes): 65656After Freeing result 6, we ' re using (in bytes): 65700After query #7, we ' re using (in bytes): 65780After freeing ResU Lt 7, we ' re using (in bytes): 65824After query #8, we ' re using (in bytes): 65904After Freeing result 8, we ' re using TES): 65948After Query #9, we ' re using (in bytes): 66028After Freeing result 9, we ' re using with (in bytes): 66072After Closing the connection, we ' re u Sing (in bytes): 65108Peak memory usage for the script (in bytes): 88748

What we did was find some suspicious actions that occurred while executing the script, and then tweak the script to give us some understandable feedback. We ran the script again, using memory_get_usage() the view memory usage changes during each iteration. Based on the growth of the allocated memory value, it implies that we have created a vulnerability in a location with a script. Because mysql_free_result() the function does not free memory, we can assume that mysql_query() memory is not allocated correctly.

Back to top of page


PHP version V5.2 includes some great new tools that can help you gain a better insight into your script's system memory allocations, as well as regain full control over the precise tuning of memory management. When used effectively, the new memory management tool will support your debugging efforts and regain some system resources.



  • You can refer to the original English text on the DeveloperWorks global site in this article.

  • Read the PHP V5.2 release notes.

  • "How to Manage memory in PHP" is an excellent article on the practical application of PHP in the management of RAM.

  • There are many documents for Memory manager functions in the Zend Developer Zone.

  • Access php.net for PHP documentation.

  • Article "A step-by-step how-to guide to install, configure, and test a Linux, Apache, Informix, and PHP server" contains a The part of the PHP parser to translate.

  • Review the error reports generated by the actual sample.

  • Learn how to migrate code developed in PHP V4 to V5 in the PHP V5 Migration Guide.

  • To get a tutorial on learning to program with PHP, check out the DeveloperWorks "Learn PHP" series.

  • Planet PHP is the PHP Developer Community News Resource.

  • Php.net is a resource for PHP developers.

  • Check out "PHP recommended reading list".

  • Browse all PHP articles and PHP tutorials on developerWorks.

  • Consult the IBM developerWorks PHP Project Resource Center extension PHP tips.

  • Listen to interesting interviews and discussions with software developers and be sure to visit the DeveloperWorks podcast.

  • Keep an eye on DeveloperWorks technical events and webcasts.

  • Check out recent seminars, trade shows, webcasts and other events for IBM open source developers that will be held globally.

  • Visit the DeveloperWorks Open source software technology zone to get rich how-to information, tools, and project updates to help you develop with open source technology and use it with IBM products.

  • Visit the Safari Online bookstore to browse the various sources of open source technology.

Access to products and technologies

    • Build your next development project with IBM trial software that can be downloaded or obtained from a DVD.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.