4 months ago by Gary Rawlings

What exactly is GC-Free?

W1siziisijiwmtkvmdcvmdkvmtqvndkvmtqvnje4l2rhbi1nb2xkltrfamhetzu0qllnlxvuc3bsyxnolmpwzyjdlfsiccisinrodw1iiiwinzuwedq1mf4ixv0

Nope, its not the latest health food hitting the west coast, but instead came up as a requirement from one of our Banking clients who were looking for a Java Engineer to help them on a green-field development project where system performance was very important.

To understand what GC-Free is properly, you first need to start with the broader context of memory management. Put simply, this is the way in which software programs leverage the memory of the computer to perform their tasks.  From a programming perspective, memory takes two forms, the stack and the heap.

The stack is a data structure that is used to store the details of the sequence of function calls made and the static variables defined within those functions. It operates in a LIFO manner, meaning the last entry added to the stack will be the first entry to be removed. As its used in this nature, the stack is fairly small in size (e.g. typically 8MB on Linux), extremely fast to access and cleans itself up as the program executes.

The heap on the other hand is where memory is allocated dynamically as you create data structures & objects in your code. So for example, if your code needs to read a file of data, perform some operation and write the results back to a new file, then typically you would load the entire contents of the file into a data structure and all of this would be stored in the heap. For this reason, the heap is much larger than the stack, but significantly slower to access and you need to free up the heap after you’ve used it.

In languages like C/C++ you have significant control over the allocation and de-allocation of memory, giving you the ability to do things like:

  • Creating objects on the stack
  • Writing your own custom memory allocators that are optimised for performance
  • Write highly optimised assembly code that can run in-line with the C/C++ code

Its for these reasons, and many others, that C/C++ is often used when you need to build systems that are ultra-low latency.

However, in JVM languages like Java, the JVM will do your memory management for you. The challenge though when writing highly performant code in Java is that GC can kick-in anytime. Garbage Collection (GC) is where the JVM is runs through all of the objects that have been allocated and to figure out which ones are no longer used so that it can de-allocate them and free up space. The issue is that GC is a very expensive operation from a CPU perspective and can severely hinder the performance of your code.

So GC-free programming is about how to optimise your code to much more tightly control the allocation & de-allocation of memory. The extent to which you need to do this is governed by the throughput or latency you need to achieve, but typically for low latency trading applications you are looking at 2-10 microseconds.

There are many things you can do in Java to optimise GC, a few examples include:

  • Using primitive types & collections as opposed to wrappers
  • Recycling your own data structures for re-use if they are mutable (e.g. ByteBuffer)
  • Using GC-free libraries / parsers

Common when doing low-latency programming is the use of profilers that allow you to see both CPU usage and memory profiling at a method call level so you see where further tuning is required.

We’ve only just scratched the surface of this topic but it helped give the team a better understanding of how to assess the candidates we presented for the role, something we pride ourselves on here.

 

Originally published here.