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.