PHP kernel Exploration: The memory management begins

Source: Internet
Author: User
Tags php script

Memory is one of the most critical parts of a computer, is a temporary storage of programs and data space, the CPU only a limited number of registers can be used to store computing data, and most of the data is stored in memory, the program runs in memory. Like CPU computing power, memory is also a critical part of determining computational efficiency.

The resources in the calculation are mainly: CPU compute power, memory resources and I/O. In order to make full use of the resources, modern computers have a multitasking operating system, which can share the CPU computing resources through the process scheduling, and share the memory storage capacity through virtual storage. The memory management in this chapter does not describe the operating system level of virtual storage technology, but focuses on the application level: how to efficiently utilize limited memory resources.

Currently, in addition to using low-level programming languages such as C + +, many programming languages have moved memory management to the language, such as Java, various scripting languages: Php/python/ruby, etc., and the cost of manually maintaining the memory is very high. These scripting languages or new languages focus on specific areas, freeing programmers from memory management to focus on the implementation of the business. Although the programmer does not need to maintain the memory manually, and in the process of running the memory of the use of management, memory management of the programming language to implement the programmer's work.

The main task of memory management is to use memory as efficiently as possible.

The use of memory includes requesting memory, destroying memory, modifying the size of the memory, and so on. If the application of memory after use is not released in a timely manner may result in memory leaks, if this happens in the resident program, over time, the program will be the memory of the machine to consume light. So for a language like PHP that does not have low-level memory management, memory management is a critical module that determines the execution efficiency of a program on a large program.

At the level of PHP, defined variables, classes, functions, and so on are involved in the application and release of memory, such as variables may be destroyed after the scope, the temporary data generated during the calculation will have memory operations, such as class objects, Data such as function definitions will not be released until after the request has been completed. In this process, it is critical to apply memory appropriately to release memory. PHP from the beginning of a set of its own memory management mechanism, before 5.3 using the classic reference counting technology, but the reference technology has some technical shortcomings, after PHP5.3, introduced a new garbage collection mechanism, so far, PHP memory management mechanism is more perfect.

In a sense, resources are always limited, computer resources are the same, the measurement of a computer processing energy in a number of indicators, but also according to different application needs will have different indicators, such as 3D games on the graphics card there are some requirements, and the Web server on the throughput and response time requirements, usually CPU, Memory and hard disk reading and computing speed have a decisive role, at the same time these resources are limited, really because of the limited we need to use them rationally.

Memory management of the operating system

When your computer's power is turned on, the software may already be using memory, no matter what operating system you are using. This is determined by the structure of the computer, the operating system is also a software, but it is a more special software, the management of all the resources of the computer, the general application and operating system is a bit like teachers and students, teachers usually manage everything, and student behavior will receive the teacher or school restrictions, For example, applications cannot directly access physical memory or other hardware resources.

The operating system directly manages the memory, so the operating system also needs memory management, memory management is so important that the computer usually has a memory management unit (MMU) to handle the CPU memory access.

Memory Management for application tiers

Because the computer's memory is managed by the operating system, the normal application is unable to access the memory directly, the application can only request memory from the operating system, the usual application is doing the same, when needed through a library function like malloc to request the operating system memory, In some performance-demanding scenarios where frequent use and release of memory is required, such as Web servers, programming languages, and so on, the system calls and normal application-layer function calls are very different due to the application of memory space to the operating system, because system calls switch the CPU from the user state to the kernel. Because of the physical memory involved, only the operating system can proceed, and the cost of such a switchover is very large, if the frequent switching between the kernel state and the user state will produce performance problems.

Given the overhead of system calls, some performance-demanding applications typically use their own memory management in User configuration, such as the first request for a slightly larger amount of memory to reserve, and the memory that is freed is not immediately returned to the operating system, such as memory can be reused, which can also avoid multiple memory requests and releases.

PHP does not need to explicitly manage the memory, which is done by the PHP interpreter. Within PHP, there is a memory management system that automatically frees memory garbage that is no longer in use, as described in the sections that follow.

Memory-related features in PHP

Some readers may have encountered a mistake like the following:

Fatal error:allowed memory size of X bytes exhausted (tried to allocate Y bytes)

This error message is very clear, PHP has reached the maximum allowable memory, usually it is very likely that our program written some problems. For example: a one-time reading of large files into memory, or the occurrence of large arrays, or in the cycle is not in a timely manner to release the unused variables, which may cause memory consumption is too large and terminated.

PHP default maximum memory usage size is 32M, if you really need to use more than 32M of memory can modify the following configuration of the php.ini configuration file:

Memory_limit = 32M

If you cannot modify the PHP configuration file, and your PHP environment does not disable the Ini_set () function, you can also dynamically modify the maximum memory consumption size:

<?php
Ini_set ("Memory_limit", "128M");
?>
Since we can dynamically adjust the maximum memory usage, is there a way to get the current memory footprint? The answer is yes.

Memory_get_usage (), this function is to get the current size of the PHP script used by the memory.
Memory_get_peak_usage (), this function returns the peak memory used by the current script to its current location, which may obtain memory requirements for the current script.
In terms of the functionality provided by PHP user space, we seem unable to control the use of memory, only passive acquisition of memory, so we learn what is the use of memory management?

The previous chapters have descriptions of reference counts, function tables, symbol tables, constant tables, and so on. When we understand that this information will take up memory, we can intentionally avoid unnecessary waste of memory, for example, we usually use autoload in the project to avoid the use of the class will not be included in, and this information is memory-intensive, If we unset the unused variable in time, it may release the space it occupies,

PHP kernel Exploration: The memory management begins

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.