Take a look at ByteBuffer#allocateDirect(int)
. It allocates a byte buffer (to which you can read and write) outside the heap (and thus outside the CG area of interest).
What it does in the background is delegating calls to com.sun.Unsafe
with some additional checking of -XX:MaxDirectMemorySize
JVM option. The -XX:MaxDirectMemorySize
says how much of direct memory one can use for direct allocation. (by default it's 64MB IIRC).
This is what Ehcache and Terracota do to avoid extensive GC.
However, this does not mean, you do not have to change your application to use it. Notice, the allocated byte buffer is just a contignous area in the memory, with no interface other than read/write value at position. So to make it accessible via, say, a Map
interface, you would have to implement this map yourself.