8 questions to the PHP memory usage

Serhii Shkarupa
4 min readAug 13, 2023

--

I endeavored to discover the most efficient ways to utilize memory while working with PHP. This led me to question whether these methods are effective or not. Let’s redirect these inquiries towards PHP’s memory usage and explore them through a series of questions.

Excuse me, I couldn’t find the 9th and the 10th questions…

Does cloning objects prevent doubling the object in the memory?

No, cloning objects in PHP does not necessarily save memory usage; in fact, it can consume additional memory. When you clone an object, you are creating a new instance that is a copy of the original object. This means that both the original object and the cloned object occupy memory in the PHP runtime.

Cloning can be useful when you need to create a duplicate of an object to work with separately or to modify without affecting the original object. However, it’s important to be aware that cloning itself does not inherently reduce memory usage.

In PHP, objects are generally assigned by reference. When you assign an object to a variable or pass it as an argument to a function, you’re essentially creating a reference to the same object in memory, rather than creating a new copy of the object’s data. This means that if you modify the object through one variable, the changes will be reflected in all references to that object.

Do generators do magic?

Yes, generators can save memory usage in PHP. This is because generators do not create new objects for each iteration of a loop. Instead, they reuse the same object for each iteration.

Are PHP iterable classes more memory-efficient than arrays?

No, PHP iterable classes can be more memory-efficient than arrays in certain scenarios, particularly when dealing with large datasets, since they allow you to work with data iteratively without loading all of it into memory at once. Iterable classes can use generators, which yield data on-the-fly, potentially reducing memory consumption compared to loading entire arrays, but it is the win of generators.

Here’s an example that demonstrates memory usage by comparing an iterable class using generators with a traditional array when processing a large dataset:

Both takes around 56Mb for storing data, but IteratorAggregate can use generator for processing data without doubling the data array to the memory, this is the advantage.

Does passing parameters by reference prevent duplicate the argument in the memory?

Yes. This is because the reference to the argument is passed, not the actual value of the argument.

Does using functions instead of loops save memory?

Yes. This is because functions are only loaded into memory once, while loops are loaded into memory each time they are executed.

In the first case, a new loop will be created for each iteration of the foreach() loop. This means that PHP will have to load the foreach() loop into memory 100 times.

The second case will only create one function, which will be used for each iteration of the foreach() loop.

In this code, the printNumber() function is only loaded into memory once. This means that PHP will only have to load the printNumber() function into memory once, even though it is used 100 times.

In general, using functions instead of loops can save memory in PHP. However, it is important to note that this is not always the case. If the function is very complex, it may use more memory than a loop. It is important to benchmark your code to see if using functions instead of loops actually saves memory in your specific case.

Does singleton pattern is the panacea for creating objects?

No, the singleton pattern is a design pattern that can be used to ensure that only one instance of an object is created. This can be useful for objects that need to be globally accessible or for objects that are expensive to create. However, the singleton pattern should not be used as a panacea for creating objects. In some cases, it may be more efficient to create multiple instances of an object.

Can streaming responses be used to minimize memory usage in PHP?

Yes, streaming responses is a technique that can be used to send content in smaller chunks rather than loading the entire content into memory. This can be a good way to minimize memory usage when generating large files for download.

Here is an example of how to use streaming responses to minimize memory usage:

Is SplFixedArray more memory-efficient than regular arrays?

Yes, SplFixedArray is a fixed-size array, which means that it cannot be resized. This can be a disadvantage in some cases, but it also means that SplFixedArray uses less memory than a regular PHP array.

If you know that you will need to use an array with a fixed size, then SplFixedArray can be a good way to minimize memory usage. However, if you need to be able to dynamically add or remove elements from an array, then SplFixedArray is not a good choice.

Here is an example of how to use SplFixedArray to minimize memory usage:

All test were done with PHP -v 8.1.

Correct me where/if i am wrong.

I hope you found something interesting and new for yourself here!

--

--