Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java memory and Windows

Noticed something today which kind of makes sense but I can't exactly explain the semantics.

Basically I create a plain old java main method and in it have a never ending while loop. In this loop I create some Strings and put them in a HashMap. All I really want is a process that runs and builds up it's memory utilisation over a period of time.

public class Test {

public static void main(String[] args) throws InterruptedException {
    final HashMap<String, String> names = new HashMap<String, String>();
    while(true) {
        names.put(new Date().toString(), "lskjflksjdflksjdflkjsieurlskjflksn,kdsgkjsdlkfjslkdjfs");
        Thread.sleep(50);
    }
}

}

The process is started with -Xms512m -Xmx512m.

Once started I can use procexp.exe to view my java process. The bits I'm trying to understand are Virtual Memory and Physical Memory. The Virtual Memory (Private Bytes) seems to map to the requested jvm size i.e 512MB. From the profile of the process over time I'm assuming that the Physical Memory (Working Set) is the actual memory in use by the process as it creeps up over time as my process generates more String values and fills up the map. It's always a lot less than 512MB.

So my question is, why is Java using Virtual Memory?

Isn't it all in RAM i.e Physical Memory?

Because I'm using Virtual Memory, does that mean it's swapping to disk?

Is that bad for performance?

Is there a way for force it all to be in RAM for better performance?

Any good articles on this kind of thing on Windows would be great.

like image 858
imrichardcole Avatar asked Apr 09 '26 11:04

imrichardcole


1 Answers

Why is Java using Virtual Memory?

All user applications interact with the OS memory using Virtual Memory. It is up to the OS to map this back to physical RAM/disk (via the page table). Because you started the JVM with -Xms512m, it will request 500 MB of Virtual Memory space when it starts up, but the OS chooses not to reserve 500MB of physical RAM for this because it may never be used.

Isn't it all in RAM i.e Physical Memory?

Unless your machine is using a high percentage (>75% or so) all of its physical RAM and the JVM based app has accessed some of its data recently, the JVM is going to be all in RAM.

Because I'm using Virtual Memory, does that mean it's swapping to disk?

No. See above.

Is that bad for performance?

Paging could be bad for performance in an app that has its data paged to disk. Windows will try to minimize this impact in a couple of ways:

  1. The virtual memory pages that are sent to disk are the least recently used ones
  2. There are several optimizations that are designed to pull a page back from disk if Windows can detect that it is going to be used soon (i.e. if you start iterating through the keys in your map, then Windows will try to pull back the pages containing the next block of keys before the iteration reaches those)

Also, as I mentioned, you are most likely not paging to disk.

Is there a way for force it all to be in RAM for better performance?

You can turn off the page file in windows. Here's a Youtube video with instructions.

Any good articles on this kind of thing on Windows would be great.

like image 105
Ryan Gross Avatar answered Apr 12 '26 01:04

Ryan Gross



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!