I have came across several times with the term "zero-allocation" and I was looking some clarification on the subject.
When "zero-allocation" is mentioned, is it referring to programs that use little allocation or allocate everything at start-up time, for example? Because it seems to me that allocating no objects at all is unfeasible in a non-trivial program, but I might be wrong.
On the other hand, using off-heap memory, is that also considered "zero-allocation" and in this case, "zero-allocation" would mean no memory allocated to be handled by the Garbage Collector?
I first heard about this in the context of this presentation: http://www.infoq.com/presentations/panel-java-performance, around 15:35.
If you have a very tight and hot loop (i. e. a loop which is ran thousands if not millions a time in very short time), then it makes sense to move allocation outside the loop.
I wrote a simulation in Java ten years ago. There was an object list being manipulated in the loop. The loop was run thirty times a second and should complete within 30 milliseconds, and yet manipulate up to fifty thousand objects. A difficulty was that objects are created and deleted in a loop iteration.
We realized soon that we should avoid object allocation (and by consequence garbage collection). We solved this problem with a zero allocation approach within the loop. How?
We replaced the list by an array of flyweight objects. The array of fifty thousand objects is allocated before the loop starts. The second trick is using a variant of the flyweight pattern. Instead of deleting and creating objects in the loop we started with fifty thousand pre-allocated objects and added a flag to mark them as "active" or not "active". Whenever we wanted to remove an object we marked it as inactive. There were many such little tricks to avoid allocation.
And it helped! The simulation was able to run in realtime and without garbage collection jitter (sudden drops of the frame rate because of a major garbage collection run).
This is a little example to show you how zero allocation might work and why it is neccessary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With