brokendreams
brokendreams

Reputation: 877

How to do user control cache eviction/ garbage collection in java?

I have a situation where there are 2 files A and B, and data is being written continuously in both of them (like a stream).

Now I know that both files A and B are going to be competing for memory and the garbage collector is going to decide what page for what file will be replaced.

I want to control garbage collection by making the garbage collector favor file A (i.e. garbage collector should always choose eviction of pages of file B compared to A). Other possibility is to force writing of file B to disk instead of caching in memory.

Can these things happen in java?

Upvotes: 0

Views: 908

Answers (4)

Speakjava
Speakjava

Reputation: 3392

The description of your problem lacks certain details, specifically, are the writes to your files sequential or is there random access involved?

As geneSummons correctly points out, you have memory management in the JVM confused with that of the Operating System. Even sun.misc.Unsafe will not allow you control over paging activity at the OS level from a Java application.

What you may want to look at is using memory mapped files, but that does depend on whether you are using random access for your writes. If all you're doing is writing sequentially this is most likely no use. Although this does not give you control over the paging of the files at the OS level it may provide you with a more efficient way of solving your problem.

There is a useful article on this subject, https://howtodoinjava.com/java-7/nio/java-nio-2-0-memory-mapped-files-mappedbytebuffer-tutorial/

Upvotes: 0

geneSummons
geneSummons

Reputation: 925

I suspect you are confusing memory management with garbage collection. Yes, garbage collection is a form of memory management, but it's not what you are talking about when discussing "which pages of memory will be swapped out to disk when memory space is low" That's not garbage collection because there are still active references to the A and B files. The Garbage Collector won't do anything until there are no references to an object.

You want to control memory page swapping not garbage collection. I'm sure I'll be corrected in comments if I'm wrong about this, but I don't think you can control in Java which pages of memory get swapped to disk when available memory is low.

Upvotes: 1

ArcherGilly
ArcherGilly

Reputation: 149

Java has automatic garbage collection and identifies which objects are in use and which are not, and deleting the unused objects.

A good source about garbage collection within Java is here

Upvotes: 0

Pritam Banerjee
Pritam Banerjee

Reputation: 18923

You cannot forcefully ask Java to do garbage collection.

But you can call System.gc() to request the JVM to do a garbage collection.

To make sure an object is ready for garbage collection you can assign it to null. That way you can make sure that when the garbage collector runs it gets this object and is removed from the heap.

Upvotes: 1

Related Questions