Search code examples
javagarbage-collectionmemcachedterracottaspymemcached

How to use extra RAM of my box on existing java app ( not exposing it to GC ) or to a third party tool I use?


These days we have lots of cheap RAM available but we cant use it as it causes huge garbage collection and halting the application which is not desirable. When I was reading Terracotta; I felt that this is solving my problem because I will use my extra memory on terracotta and that memory will not be exposed to garbage collection.

Is it that in my application I can create some distributed hashmap and the data I put on this hashmap will not be counted on my java heap size ? But that will mean code changes. And this looks same like MemCached.

Is there any way possible; so that without making any code changes to my existing app ( or if I am using a third party like jmeter ) I can use this extra RAM ?

If it is not possible with terracotta; is it possible in any other way ?


Solution

  • 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.