memory_get_usage() - Memory Usage Info

This section provides a tutorial example on how to the memory_get_usage() function to get memory usage information. ini_set('memory_limit', '256M') can be used increase memory limit. unset($o) can be used from objects from memory.

If you are running PHP scripts that use large objects, repeating loops, and/or recursive calls, you may experience system memory issues. There are several functions that allows you to query and manage system memories:

Here is simple PHP script to demonstrate above functions:

<?php
# memory-test.php
#- Copyright (c) HerongYang.com. All Rights Reserved.

echo "Memory usage report:"."\n";
$total = memory_get_usage(True);
$used = memory_get_usage(False);
echo "   Total memory: ".$total."\n";
echo "   Used memory: ".$used."\n";

echo "Memory test on string object (10Ki) allocatin:"."\n";
$before = memory_get_usage();
$o = str_repeat("Hello PHP!", 1024); # 10,240 byte-string
$after = memory_get_usage();
unset($o);
$final = memory_get_usage();
echo "   Memory used before allocation: ".$before."\n";
echo "   Memory used used by the string: ".($after-$before)."\n";
echo "   Memory freed up by the string: ".($after-$final)."\n";

echo "Increase memory upper limit:"."\n";
$before = ini_get('memory_limit');
ini_set('memory_limit', '256M');
$after = ini_get('memory_limit');
echo "   Memory limit before: ".$before."\n";
echo "   Memory limit after: ".$after."\n";
?>

Below is the output of the above script on my Linux computer:

herong$ php memory-test.php
Memory usage report:
   Total memory: 262144
   Used memory: 227512

Memory test on string object (10Ki) allocatin:
   Memory used before allocation: 227600
   Memory used used by the string: 10536
   Memory freed up by the string: 10264

Increase memory upper limit:
   Memory limit before: 128M
   Memory limit after: 256M

Note that:

If you get a memory overflow error message, you should view the reported memory size and object size that caused the overflow. For example, the following error message says that PHP engine failed to allocate an object of 12,288 bytes (a small object) at line 1,291. So the code before this line has consumed almost the entire memory of 134,217,728 bytes. You need to review the code to ensure there is no memory leaks.

Allowed memory size of 134,217,728 bytes exhausted (tried to allocate
12288 bytes) in some-script.php on line 1291.

However, the following error message says that PHP engine failed to allocate an object of 331,776 bytes (a large object) at line 1,291. So maybe this time, this large object is questionable. Why this object is so big?

Allowed memory size of 268435456 bytes exhausted (tried to allocate
331776 bytes) in other-script.php.php on line 159.

Table of Contents

 About This Book

 Introduction and Installation of PHP 7.3

 PHP Script File Syntax

 PHP Data Types and Data Literals

 Variables, References, and Constants

 Expressions, Operations and Type Conversions

 Conditional Statements - "if" and "switch"

 Loop Statements - "while", "for", and "do ... while"

 Function Declaration, Arguments, and Return Values

 Arrays - Ordered Maps

Interface with Operating System

 $argv[] - Command Line Arguments

 Options to Execute External Programs

 `command` - Backtick Operator

 exec() - Execute External Programs

 system() - Execute External Programs

 passthru() - Execute External Programs

 popen() - Execute External Programs

 proc_open() - Execute External Programs

memory_get_usage() - Memory Usage Info

 Introduction of Class and Object

 Integrating PHP with Apache Web Server

 Retrieving Information from HTTP Requests

 Creating and Managing Sessions in PHP Scripts

 Sending and Receiving Cookies in PHP Scripts

 Controlling HTTP Response Header Lines in PHP Scripts

 Managing File Upload

 MySQL Server Connection and Access Functions

 Functions to Manage Directories, Files and Images

 SOAP Extension Function and Calling Web Services

 SOAP Server Functions and Examples

 Localization Overview of Web Applications

 Using Non-ASCII Characters in HTML Documents

 Using Non-ASCII Characters as PHP Script String Literals

 Receiving Non-ASCII Characters from Input Forms

 "mbstring" Extension and Non-ASCII Encoding Management

 Managing Non-ASCII Character Strings with MySQL Servers

 Parsing and Managing HTML Documents

 Configuring and Sending out Emails

 Image and Picture Processing

 Managing ZIP Archive Files

 Managing PHP Engine and Modules on macOS

 Managing PHP Engine and Modules on CentOS

 Outdated Tutorials

 References

 Full Version in PDF/EPUB