user826323
user826323

Reputation: 2338

Garbage collection + memory paging

I am trying to understand the collaboration between GC inside JVM and the memory paging. I have a linux box which has 4GB RAM and allocated 1.2GB for JVM. If I run an application which requires heavy memory, I am wondering when the memory paging happens and when it actually throws out of memory error. when an object is created, it is created on the heap and will be garbage-collected if it is not used or alive if it is used by an application.

So my question is when does the OS start memory paging for JVM and what happens to the live object that is paged by OS?

And also if the OS pages live java objects, how does it prioritize which object should be paged first? does it page the object that occupies most of memory?

Upvotes: 3

Views: 1906

Answers (2)

Pierre Laporte
Pierre Laporte

Reputation: 1215

Short answer is : the JVM has absolutely no way of knowing that the OS swaps the heap, or from preventing it from swapping.

To be more precise, you may turn on JVM flags to prevent swapping (-XX:+UseLargePages), but if the OS runs out of large pages, it will revert to regular swapping. Also, any GC cycle will force every page to be fetched into RAM so that it can be inspected. So basically, swapping + Full GC = you are going to run into a hell of a Stop-The-World.

G1 will have a slightly different behaviour, since it will collect first the memory regions that are filled with dead objects, but it will still take more time due to page faults.

Hope that helps !

Upvotes: 2

Jatin
Jatin

Reputation: 31724

Firstly Memory Paging is handled by the underlying OS and not by the JVM.

If you meant de-fragmentation, then yes JVM does it for you. This gives more insight on how it is handled by the Garbage Collector which transfers long-lived objects to different generations and thus de-fragments on the go. This obviously affects which objects are paged out/not. But this is the max indirect-control what you have (by prolonging or shortening the life of the object)

And as per the memory heap, it is purely when there is no more space left.

Upvotes: 1

Related Questions