PHP fatal error allowed memory size of 134217728 bytes exhausted


Introduction

The “php fatal error allowed memory size of 134217728 bytes exhausted” error is a PHP error that occurs when PHP runs out of memory. This can happen when PHP is trying to load a large file or when there is a lot of traffic to your website.

What is a memory leak?

A memory leak is a type of resource leak that occurs when a computer program inappropriately manages memory allocations so that memory no longer needed is not released. A memory leak may also happen when an object is stored in memory but cannot be accessed by the running code.

How to solve the memory leak?

A memory leak is when a program fails to release memory that it no longer needs. This can cause the program to run out of memory, and eventually crash.

There are a few ways to solve a memory leak:

-Restart the program: This will free up all of the memory that was being used by the program.

-Increase the amount of memory available to the program: This will allow the program to use more memory, and hopefully avoid running out of memory.

-Find and fix the code that is causing the memory leak: This is usually the best solution, as it will solve the problem permanently.

How to prevent memory leaks?

Preventing memory leaks can be difficult, but there are a few things you can do to reduce the chances of them occurring in your code. One way to prevent memory leaks is to use object-oriented programming techniques and design patterns that promote the use of objects that are short-lived and have well-defined lifetimes. For example, the Singleton pattern ensure that only one instance of a class is created, which can help avoid memory leaks by ensuring that unused objects are not kept in memory.

Another way to prevent memory leaks is to use language features that help manage memory automatically, such as reference counting or garbage collection. For example, C++11 introduced a new feature called smart pointers which are objects that automatically manage the lifetime of the object they point to, including deallocating them when they are no longer needed. This can help avoid memory leaks by ensuring that unused objects are not kept in memory.

Finally, it is important to test your code for memory leaks before releasing it. You can use tools such as Valgrind orElectric Fenceto detect leaked memories and pinpoint the location of the leak in your code so you can fix it.

Conclusion

If you are bumping into the “allowed memory size of bytes exhausted” error, it means that your script is using more memory than what is allowed by your server’s configuration. To fix this, you will need to increase the memory limit in your php.ini file.

First, open your php.ini file and look for the following line:

; Maximum amount of memory a script may consume (128MB)
memory_limit = 128M

Change the number to a higher value, such as 256M:

; Maximum amount of memory a script may consume (256MB)
memory_limit = 256M

Save the file and restart your server for the changes to take effect.


Leave a Reply

Your email address will not be published. Required fields are marked *