Alex Rivera | Logout

Does Java garbage collection starts to work to prevent memory swapping?

Asked 2010-08-23T11:27:26.773
12

As an example lets say that I set the JVM's maximum heap to 4GB. However, once my application reaches about 3GB, the OS starts to swap some memory to disk. At this point there are several objects already out of scope, and instead of requesting more memory the JVM could first garbage collect old objects. In terms of performance it would be better to have garbage-collection run than to do memory swapping. Is the JVM garbage-collection smart about situations like this or is it completely unaware of it? Can we somehow tune the JVM to address this situation?

I know that there's a chance that the garbage-collection will run before we reach the 3GB and therefore we never actually need to swap memory but that doesn't really answer my question.

EDIT: lets assume that my machine has more than 4GB of memory but sometimes there are other applications taking some of that memory leaving me with less than 4GB. I would rather not have to reduce the maximum heapsize given that most of the times I will have the 4GB but I was wondering if the GC would be smart enough in the other situations.

Edit
Report

1 Answer

2

The GC is unaware of this situation. You should not specify a heap that's bigger then the free amount of memory on your machine. The solution is to reduce the heapsize, and make sure no other programs eats all memory.

Actually do not think there is a easy way even in C to check if the process is swapping (Maybe there is, if so, please add a comment).

answered 2010-08-23T11:36:19.857

Your Answer