Home Backend Development PHP Tutorial How to get memory usage method in PHP

How to get memory usage method in PHP

Feb 27, 2018 am 09:54 AM
php use Condition

PHP built-in function memory_get_usage() can return the amount of memory currently allocated to the PHP script, in bytes. In actual WEB development, these functions are very useful, and we can use them to debug PHP code performance.

The memory_get_usage() function returns the memory usage, the memory_get_peak_usage() function returns the peak memory usage, and getrusage() returns the CUP usage. But please note that these functions need to run on Linux.

Let's look at an example below:

echo '开始内存:'.memory_get_usage(), ''; 
$tmp = str_repeat('hello', 1000);   
echo '运行后内存:'.memory_get_usage(), '';  
unset($tmp);   
echo '回到正常内存:'.memory_get_usage();
Copy after login

Output result:

开始内存:147296 
运行后内存:152456 
回到正常内存:147296
Copy after login

In the example, we use str_repeat() to repeat the string "hello" 1000 times. Finally, To compare the memory size consumed before and after. As can be seen from the above example, in order to reduce memory usage, you can use the unset() function to delete variables that are no longer needed. Similar to the mysql_free_result() function, when we no longer need the result set obtained by querying data, we can use it to release the memory occupied by the query.

The function memory_get_usage() can also have a parameter, $real_usage, whose value is a Boolean value. If set to TRUE, obtains the actual memory size allocated by the system. If unset or set to FALSE, emalloc() will report the amount of memory used.

In actual WEB development, you can use PHP memory_get_usage() to compare the memory occupied by each method to choose which method takes up less memory.

The number of bytes returned by function memory_get_usage() (unit: byte(s)). The following custom function converts the number of bytes into MB for easier reading:

function memory_usage() { 
    $memory     = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).'MB'; 
    return $memory; 
}
Copy after login

Commonly used methods for debugging and detecting PHP code performance are:

memory_get_usage can analyze the memory footprint.

Use the microtime function to analyze the program execution time.

Related recommendations:

PHP detection to obtain memory information_PHP tutorial

##PHP obtains memory usage through the built-in function memory_get_usage()

Program code for obtaining memory usage in PHP

The above is the detailed content of How to get memory usage method in PHP. For more information, please follow other related articles on the PHP Chinese website!

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 Article Tags

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)

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles