Kellen
Kellen

Reputation: 611

Can you exclude an object from being tracked by the garbage collector in Java?

I have a fairly unique Java service I'm working on that has a working set in the order of 50-100 GBs. Within the service I have a very complicated object graph. The vast majority of these objects live for the entirety of the process lifecycle and will never be collected.

Currently I have garbage collection times of around 10 seconds for the old generation heap. I'm wondering if there's any way to exclude these objects to speed up the performance of my garbage collection?

Upvotes: 4

Views: 1150

Answers (5)

the8472
the8472

Reputation: 43052

If you just want primitive data outside the heap then direct byte buffers should do the job.

Serializing/deserializing on-heap objects to/from native memory is also what many off-heap storage libraries do.

But they might not really be necessary with the right collector choice. CMS does not move tenured objects and can just skip large primitive arrays, so even very large heaps shouldn't incur much extra GC costs as long as they are mostly filled with primitive arrays and if you manage to tune your GC to avoid concurrent mode failures.


If you want full-blown objects outside the grasp of the garbage collector, then there is jillegal.

As its name and documentation suggests it reaches deep into VM internals and comes with a lot of caveats/forward compatibility problems.

Only use it if you feel doing open heart surgery next to a pool of sharks is the most reasonable solution to your problem.

Upvotes: 1

Gaur93
Gaur93

Reputation: 695

Garbage collector collects/frees an object in heap area when it is unreachable. So to exclude an object from being tracked by the garbage collector you should always have reference id of that object(best case will be when it is in form of strong reference)

Upvotes: 0

Dragan Bozanovic
Dragan Bozanovic

Reputation: 23552

You may want to take a look at some caching solutions which utilize off-heap data store:

The on-heap store refers to objects that will be present in the Java heap (and also subject to GC). On the other hand, the off-heap store refers to (serialized) objects that are managed by EHCache, but stored outside the heap (and also not subject to GC). As the off-heap store continues to be managed in memory, it is slightly slower than the on-heap store, but still faster than the disk store.

Upvotes: 1

Joop Eggen
Joop Eggen

Reputation: 109567

Invert the logic, and put the graph in a separately running database. And then operate with more volatile queries. Whether this must be a graph database depends (- the commercial Neo4J?).

You may pay a bit of speed with having persistence and in case of a graph database a fast query language & implementation.

Upvotes: 0

OldCurmudgeon
OldCurmudgeon

Reputation: 65821

The only way I can think of to would be to reduce the number of Objects in the heap. You may be able to achieve this by taking direct control of the graph. Perhaps allocate yourself a chunk of memory as a large int[] and allocate/free space in there. Could you post some code that demonstrates the structure of the nodes in your graph? Perhaps we could give more concrete suggestions.

Upvotes: 1

Related Questions