Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 9, 2021 03:34 pm GMT

PHP Memory Usage and Performance Improvements Tips

Memory usage and performance improvements are things that makes everybody happier, from end user to cloud and infrastructure engineers. And they are all right and this is a optimization that we should try to achieve as much as possible.

I am also keeping this page for a reference to my future self, because we cannot rely too much on our memory and that will be a good reference I want to re-visit. I will make constant updates on this page. Let's have fun.

Arrays has a larger footprint in order to avoid constant memory pointers reassignments. It then reserve large ammounts of memory when more elements or indexes are added.

Garbage collector is working as expected when the internal reference count (how may times a value is used) reaches zero:

$x = "foobar";  // refcount = 1$y = $x;        // refcount = 2unset($x);      // refcount = 1unset($y);      // refcount = 0 -> garbage collector will be happy ==> Destroy!

But self-referencing can be tricky:

$x = [];            // refcount = 1$x[0] =& $x;        // refcount = 2unset($x);          // refcount = 1                    // It will never come to zero due to cycle

Cycle collector will eventually destroy it, but it will hang on memory for a while anyways.

PHP Benchmarking

PHPBench.com was constructed as a way to open people's eyes to the fact that not every PHP code snippet will run at the same speed. You may be surprised at the results that this page generates, but that is ok. This page was also created so that you would be able to find discovery in these statistics and then maybe re-run these tests in your own server environment to play around with this idea yourself, by using the code examples (these code examples are automatically generated and as the code in my .php files change, so do they).


Original Link: https://dev.to/rafaelbernard/php-memory-usage-and-performance-improvements-tips-list-441e

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To