How does BigMemory hide objects from the Java garbage collector?
-
BigMemory gets its performance advantages by restricting the size of the Java Heap while still using all available physical RAM. How is this achieved? Is it done by deserializing serialized representations from the offheap store?
-
Answer:
BigMemory achieves off-heap storage using Direct ByteBuffers, which is a feature introduced with NIO in Java 1.4 to allow zero copy buffers between Java applications and the operating system as it's not possible with memory belonging to Java heap because of unstable location[1]. An interesting additional property of Direct ByteBuffers is that they give access to memory that won't be managed by the Garbage Collector, hence avoiding its overhead. This property is making it a good candidate solution for large in-memory storage. To build a whole cache solution around Direct ByteBuffers, BigMemory had to deal with two important issues : As ByteBuffers are just arrays of bytes, BigMemory has to serialize/deserialize objects to achieve off-heap storage. BigMemory deals with the potential overhead of this operation by keeping hot data in Java heap[2]. As off-heap storage isn't managed by Garbage Collector, BigMemory comes with an ad-hoc memory allocator. Although BigMemory comes with a seducing promise, it should be evaluated with care as off-heap memory isn't a magical solution for every GC problems. Furthermore, as several observers pointed it out when it was released, the Direct ByteBuffer trick isn't new[3]. [1] ByteBuffer - Direct vs. non-direct : http://download.oracle.com/javase/6/docs/api/java/nio/ByteBuffer.html [2] BigMemory - Off-heap Store : http://ehcache.org/documentation/offheap_store.html [3] Terracotta's BigMemory Aiming to Eliminate Garbage Collection for Java Caches - Comments : http://www.infoq.com/news/2010/09/bigmemory#view_60531
Michaël Figuière at Quora Visit the source
Other answers
Consider a Java HashMap, which allows you to store and later access information by key (see IDictionary in .NET for equivalent). While there is a "bucket array" in the HashMap, that is only one object, and only consumes about 6-7 bytes on average per entry. The real killer is this: Each item placed into the HashMap holds at least three objects that the GC has to look at and subsequently decide to ignore: (1) the HashMap.Entry object, (2) the key, and (3) the value. If the key and the value are Strings, then that is actually 5 objects, since each String also has a reference to its own char[] object. If the key and/or value are complex business objects, they may represent hundreds of objects per entry! So, if you can "serialize" objects and store them in a series of ByteBuffer objects, you can reduce the number of objects necessary to keep that data in memory by somewhere between 95 - 100%. Since GC pauses are largely a function of the (a) the number of objects and (b) the amount of inter-connectedness of those objects, reducing the number of objects will decrease GC time. Unfortunately, to pull them back onto the heap so that you can use them as objects, you do have to "deserialize" them, so this won't be as fast as the HashMap, for example, and the deserialization will produce garbage of its own. (Note that such garbage tends to be localized, and thus cleaned up very efficiently by a young generation GC.) The concept of BigMemory is very similar to the old (2002) off-heap storage feature in Coherence (see BinaryMap on http://tinyurl.com/m6kp8bw), which was the precursor for the Elastic Data feature for RAM & flash storage that is in Coherence today (see http://tinyurl.com/oz3dend). For the sake of full disclosure, I work at Oracle. The opinions and views expressed in this post are my own, and do not necessarily reflect the opinions or views of my employer.
Cameron Purdy
Related Q & A:
- How to count associated objects?Best solution by Stack Overflow
- How to save new objects into an arrayList?Best solution by stackoverflow.com
- How can I get objects in a S3 bucket?Best solution by Stack Overflow
- How to return multiple objects of a specific library in R?Best solution by Stack Overflow
- How can I hide how many friends I have on facebook?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.