I was recently interviewed by telephone for a contract position. It went very badly, not least because I was placed on speakerphone and could hear myself being broadcast to the room at the other end of the line with a slight delay - a terrible situation which, for me at least, makes it impossible to concentrate and string a sentence together.
Do you remember when you had to manage your own memory?
My first job as a professional programmer involved writing simulations of gas flow and pressure in pipeline networks. For better or worse I decided to create these simulations in C. How we used to laugh after discovering that the reason our simple 5-line routine caused a catastrophic failure was because we accidentally wrote a value into the 11th element of an array with only 10 available spaces! Forgive me if this is obvious to you, but there are plenty of coders out there who’ve never used
malloc in their lives. If you don’t know what I’m talking about get yourself a copy of Kernighan & Ritchie and marvel at the steam-powered code cavemen used to write.
So what is Garbage Collection?
undefined. All other values are objects. Simple, or
Where do we put the garbage to be collected?
The reason for creating a New Space and an Old Space is that objects tend to die young. As the names suggest, New Space is where new allocations happen. It is relatively small and things change quickly in New Space. It’s easy to allocate space in New Space - we simply have an allocation pointer which is incremented whenever we reserve space for a new object. New Space is small, and when we reach the end of it a scavenge (minor garbage collection cycle) is triggered. The scavenge either removes dead objects or promotes marked objects to Old Space. Old Space is where objects in the New Space that survived two garbage collections are promoted to. They are know as the Old Generation. Allocation in Old Space is fast but garbage collection is expensive so it isn’t performed here very often.
How does the Garbage Collector know what to collect?
The garbage collector must never reclaim variables or objects that are still in use and should only reclaim values that cannot be reached from a root node (global objects, DOM elements or local variables). In order to do this the garbage collector uses an algorithm known as
mark & sweep. In the marking phase, all live objects on the heap are discovered and marked. This is done by recursively traversing the list of all variables and marking any values referenced by those variables.
There are 3 marking states:
- White: an object which has not yet been discovered by the garbage collector.
- Grey: An object which has been discovered by the garbage collector but not all of its neighbours have been processed yet.
- Black: An object which has been discovered and all of its neighbours have been fully processed.
Where do I go from here?
This description has necessarily been highly simplified but it covers the main points you would need to know on a daily basis. Here are a few rules of thumb to bear in mind when coding so as to write memory-friendly code:
- This is another good reason for developing in
strictmode. Amongst other things,
strictmode highlights where local variables have been declared without using the
varkeyword which results in them being considered global variables (see the previous point).
- If you can’t avoid creating objects then try re-using them. This is know as object pooling and it is the process of creating a pool (usually an array) of objects once, calling a factory method to get an object from the pool when it is needed and then returning it to the pool once it has finished being used.
- If you’re using event listeners make sure you unbind them when they are no longer required.
It’s worth understanding garbage collection in principle and using good practice such as the tips described above as part of your everyday routine but don’t worry too much about optimising code for memory management unless or until it actually becomes a problem. Chances are you’ll be spending an awful lot of effort to achieve minimal gain because you won’t know what to optimise until you look at what is causing the problem.
In short, read this and then forget about it until you have a performance or memory problem. In that case, give this post a quick glance, refresh your memory, and go fix the problem!