MoveFast
MoveFast

Reputation: 3015

java - can we do our own memory management?

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

Upvotes: 5

Views: 3786

Answers (4)

Peter Lawrey
Peter Lawrey

Reputation: 533492

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.

Upvotes: 3

Julias
Julias

Reputation: 5892

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

Upvotes: 3

biziclop
biziclop

Reputation: 49744

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.

Upvotes: 2

Neet
Neet

Reputation: 4047

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.

Upvotes: 5

Related Questions