Tracking Memory Usage in PHP

The memory_get_usage function can be used to track the memory usage. The ‘malloc’ function is not used for every block required, instead a big chunk of system memory is allocated and the environment variable is changed and managed internally.

The two different types of memory usages are −

  • The memory required by the engine from OS (the real usage)
  • The amount of memory that was actually used by the application (internal usage)

The above mentioned memory usage can be tracked using memory_get_usage(). This function returns both real and actual memory used depending on our requirement.

For example: if we are looking at specific code snippets, internal memory might be relevant. On the other hand, if memory usage is being globally tracked, the real usage would be more relevant.

Updated on: 07-Apr-2020


Kickstart Your Career

Get certified by completing the course

Get Started