All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Ways to Check a PHP Script Memory Usage

Last Updated On Sunday, Apr 9, 2023 | Written By - Mudit Jain

Coding Tutorials

Ways to Check a PHP Script Memory Usage

If you want to learn about PHP, script memory usage isn't one of the first things you will focus on. But knowing this can be extremely useful to you, especially if you want to become a better developer.

Why is it important to check a PHP script memory usage?

Memory efficiency is often neglected when considering the requirements of web applications. Generally, this is okay because there are other more important things to consider like good practices of authentication and SQL security, for example.

But if you end up using excessive memory in your device, you can experience a number of issues. This can lead to crashes, bugs that are very difficult to find, and it might even cause Denial-of-Service attacks.

This becomes even more important in websites with high traffic where executions of concurrent scripts tend to occur. Also, memory-related problems can easily cause other issues.

How to check your usage of PHP script memory script

As a Mac user, it's important for you to take care of your device to ensure that it's always running smoothly. One thing you can do as part of the maintenance process is to check your Mac memory usage from time to time. To check this, PHP comes with a built-in function to return the amount of memory that's allotted to a PHP script. The function is:


Although this function gives back the amount of memory allotted to a PHP script, you will see the amount in bytes. If you want to convert this amount to KB, use:


If you want to convert this amount to MB, use:


Apart from using the above functions, there is another important function you can use. This time, the function will return the maximum amount of memory that a PHP script uses until you call the function. For this, you use:


You may soon discover that your device's current memory usage will go down after you unset the array while the peak usage stays the same. The reason for this is that this function takes an optional "boolean argument" with a false default value. This argument is:


What else can you do?

There might be times where you will find it too challenging to get output from the functions mentioned above. This is especially true when your device's memory usage depends on other factors like the request string values.

Sometimes, memory problems only occur after some time has passed or under very specific circumstances. In such cases, simply checking your memory usage won't suffice. For this, one thing you can do is to use a database or log file to document your device's memory usage.

For a more efficient solution, you should make PHP do the work for you. Check if your device's memory usage is exceedingly high. If it isn't, you can continue with the execution of the script.

If not, you can send a notification email to yourself that contains relevant debugging information. Doing this makes it easier for you to check your Mac's scripts actively to find out if something is wrong as soon as possible.

Setting a memory limit

If you don't want your Mac to use too much memory, you can set a memory limit for PHP using the function:


For instance, you can set your PHP memory limit this way:


Apart from setting the limit for PHP, you can also increase this limit. The memory limit of PHP is "per-script," which means that it sets a maximum amount that each script can allocate. This prevents any scripts that are poorly written from using up all of the available memory on your server.

The limit setting of PHP isn't like a simple storage space where several PHP scripts grow within or pool from. Therefore, when you block this setting, you may see resulting error outputs that look like this:

PHP Fatal error: Out of memory (allocated x) (tried to allocate x bytes) in /path/to/php/script


Fatal error: Allowed memory size of x bytes exhausted (tried to allocate x bytes) in /path/to/php/script

For instance, if you request two or more scripts at the same time, each one is completely separate from the other. In other words, the memory_limit setting isn't something that they share.

PHP doesn't support multithreading and it isn't designed for this either. This means that if several scripts use 100MB all at the same time, this would have a total of 500MN memory usage of PHP. If your PHP memory limit is 128MB, then you wouldn't hit it.

However, if you use scripts that request other inline PHP scripts using include once, require or include, the limit gets inherited and shared by all of the scripts that depend on the parent script.


Learning how to check your Mac's PHP script memory usage might not be at the top of your priority list but it is still an important thing. But knowing these processes, you can easily troubleshoot your device in case you encounter any issues.

Recommended Articles

40 Proven Ways To Increase Your Website Traffic (In 2023)So in this article which is based on how to increase your website traffic i am listing 40 proven ways which i used to generate and increase my website and get over 100,000 visitors every month on this...

Tags - Increase Website Traffic | Published On - 9 Apr 2023

How To Start A Blog (In 2023) - InDepth GuideHow to start a blog? This is the most asked question in blogging field. So do you want to start a blog. Ask these simple questions before jumping to the below article. Do you have a passion f...

Tags - Blogging | Published On - 9 Apr 2023

10 Ways To Increase Organic Traffic (In 2023)Every blogger or webmaster always wanted to increase atleast one thing and that is their website traffic. This is the most precious thing a website or blog could ever have. Imagine you continuou...

Tags - Increase Website Traffic | Published On - 9 Apr 2023

Proven Ways To Increase Social Media Traffic (In 2023)Social Media is one of the best source of marketing you can reach to billions of people from around the world. Every second person on the earth is atleast active on 1 social media. So imagine ho...

Tags - Increase Website Traffic | Published On - 9 Apr 2023

5 Ways To Make Money Online Through Blogging (In 2023)Do you also want to make money online through blogging? Let me clear this things that making money online is not an easy task many people thought. It's like any other way to earn money but in ...

Tags - Make Money Online | Published On - 9 Apr 2023