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.