5

Is it possible to perform the memory management by yourself. e.g. We allocate a chunk of memory outside the heap space so that it is not subject to GC. And we ourselves take care of allocation/deallocation of objects from this chunk of memory.

Some people have pointed to frameworks like Jmalloc/EHcache. Actually i more want to understand that how they actually do it.

I am fine with some direct approach or even some indirect approach ( e.g. first serialize java objects).

5
  • 2
    Why do you want to do this? Just curious. Commented Apr 2, 2012 at 14:47
  • The Unsafe class can possibly help you doing weird stuff... Commented Apr 2, 2012 at 14:47
  • See also: stackoverflow.com/questions/5574241/… Commented Apr 2, 2012 at 14:48
  • I have a use-case where I know a lot about the life time of items which I want to store. So I want to try out their Memory management out own my own..outside heap and GCs Commented Apr 2, 2012 at 14:49
  • See also: stackoverflow.com/questions/9050852/… Commented Apr 2, 2012 at 18:33

4 Answers 4

5

You can not allocate Java objects in a foreign memory location, but you can map memory which is e.g. allocated in a native library into a direct ByteBuffer to use it from Java code.

Sign up to request clarification or add additional context in comments.

Comments

3

You can use the off the heap memory approach Look for example jmalloc

and this is also usefull link Difference between on and off the heap

3 Comments

I more want to understand that how frameworks like jmalloc and EHCache for example are doing it.
@MonojGumber: They underlying mechanism ist most likely not written in Java, but in native code. The library then provides a Java interface.
It can be read on the jmalloc project page that they are using serialization. This is a nice idea to store large objects, but not really suitable for performance.
3

I have a library which does this sort of thing. You create excerpts which can be used a rewritable objects or queued events. It keeps track of where objects start and end. The downside is that the library assumes you will cycle all the object once per day or week. i.e. there is no clean up as such. On the plus side its very efficient, can be used in an event driven manner, persisted and can be shared between processes. You can have hundreds of GB of data while using only a few MB of heap.

https://github.com/peter-lawrey/Java-Chronicle

BTW: It supports ByteBuffers or using Unsafe for extra performance.

1 Comment

Even if you don't use it, it may give you some ideas.
2

If you mean Java objects, then no, this isn't possible with standard VMs. Although you can always modify the VM if you want to experiment (Jikes RVM for example was made for this very purpose), but bear in mind that the result won't really be Java any more.

As for memory allocation for non-java data structures, that is possible and is being done regularly by native libraries and there is even some Java support for it (mentioned in the other answers), with the general caveat that you can very easily self-destruct with it.

2 Comments

can you please give more details on allocation for non-java data structures. I am ok to convert my Objects to a serialized/byte buffer form. Will some java solution work for that?
@ManojGumber The other two answers contain hints about this: using direct ByteBuffers is one way or you can use a high-level library like jmalloc or BigMemory. But you should really only be using this in production if GC is your main bottleneck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.