How much memory does each PHP process consume?

To check memory usage, PHP provides an inbuilt function memory_get_usage(). This function returns the amount of memory allocated to a PHP script.

** memory_get_usage() function returns the amount of memory allocated to your PHP script in bytes.

To convert them in KB – memory_get_usage/1024

To convert them in MB – memory_get_usage/1048576

PHP MCQ

PHP Built-in Web-Server

How to Check Memory Usage in PHP by memory_get_usage() Method

Let’s demonstrate this through an example.

Here is a simple PHP script, which creates an instance of MongoDB.

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

<?php

/* Memory consumption before script execution */

 

echo "\n Memory Consumption is   ";

echo round(memory_get_usage()/1048576,2).''.' MB';

 

$connection = new Mongo();

 

$db = $connection->bookmarks;

 

$collection = $db->emp;

 

$obj = $collection->findOne();

 

var_dump($obj);

 

/* Memory consumption */

 

echo "\n Memory Consumption is   ";

echo round(memory_get_usage()/1048576,2).''.' MB';

?>

It is very useful to know how much memory is allocated during your PHP script execution so that you can avoid Fatal Error: Allowed memory size exhausted.

PHP-FPM helps improve your app’s performance by maintaining pools of workers that can process PHP requests. This is particularly useful when your app needs to handle a high number of simultaneous requests.

By default, Platform.sh automatically sets a maximum number of PHP-FPM workers for your app. This number is calculated based on three parameters:

  • The container memory: the amount of memory you can allot for PHP processing depending on .
  • The request memory: the amount of memory an average PHP request is expected to require.
  • The reserved memory: the amount of memory you need to reserve for tasks that aren’t related to requests.

The number is calculated as follows:

How much memory does each PHP process consume?

Note that when the resulting number is a decimal, it’s rounded up to set the maximum number of workers. Also, the minimum number of PHP-FPM workers is 2.

To adjust the maximum number of PHP-FPM workers depending on your app’s needs, follow the instructions on this page.

You need:

Note that the memory settings mentioned on this page are different from the memory_limit PHP setting. The memory_limit setting is the maximum amount of memory a single PHP process can use before it’s automatically terminated.

To determine what the optimal request memory is for your app, you can refer to your PHP access logs. Run a command similar to:

$ platform log --lines 5000 | awk '{print $6}' | sort -n | uniq -c

This command takes into account the last 5,000 requests that reached PHP-FPM. You can adjust this number depending on the amount of traffic on your site.

In the output, you can see how many requests used how much memory, in KB. For example:

    2654 2048
    431  4096
    584  8192
    889  10240
    374  12288
     68  46384

The output shows that:

  • The majority of requests peaked at 2,048 KB of memory.
  • Most other requests used up to around 10 MB of memory.
  • A few requests used up to around 12 MB of memory.
  • Only 68 requests peaked at around 46 MB of memory.

In this situation, you might want to be cautious and to 12 MB. Setting a lower request memory presents a risk of allowing more concurrent requests. This can result in memory swapping and latencies.

For further help in estimating the optimal request memory for your app, use the log analyzer tool for Platform.sh by Pixelant. This tool offers a better visualization of access logs. It also provides additional insights into the operation of your app. These can help you further optimize your configuration and provide guidance on when to increase your plan size. Note that this tool is maintained by a third party, not by Platform.sh.

By default, the request memory is set to 45 MB and the reserved memory is set to 70 MB. These values allow most programs to run, but you can amend them to fit your needs.

To do so, adjust your app configuration. Under runtime in the , set the values for reserved_memory and request_memory.

For example, if you estimate your to be 110 MB and your reserved memory to be 80 MB, you can use:

runtime:
    sizing_hints:
        request_memory: 110
        reserved_memory: 80

Note that the minimum value for the request_memory key is 10 MB and the minimum value for the reserved_memory key is 70 MB. If you set lower values, they’re automatically overridden with those minimums.

To check the maximum number of PHP-FPM workers available to your app, run the following command, where

    2654 2048
    431  4096
    584  8192
    889  10240
    374  12288
     68  46384
2 refers to PHP-FPM workers:

How much memory does PHP use?

The right one for you depends on your system configuration. A typical appropriate memory limit for PHP running Drupal is 128MB per process; for sites with a lot of contributed modules or with high-memory pages, 256MB or even 512MB may be more appropriate.

How does PHP memory limit work?

The PHP memory_limit is the maximum amount of server memory that each PHP script is allowed to consume. Per the PHP documentation: “This sets the maximum amount of memory in bytes that a script is allowed to allocate. This helps prevent poorly written scripts from eating up all available memory on a server.”

How do I check my PHP memory limit?

php $memory_limit = ini_get('memory_limit'); if (preg_match('/^(\d+)(.)$/

How to reduce memory usage in PHP?

PHP Memory Usage and Performance Improvements Tips.
Use objects with declared properties over array..
Be careful to self-referencing that would prevent garbage collector from work..
PHP Benchmarking..
PHP benchmarks and optimizations..