imrichardcole
imrichardcole

Reputation: 4695

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.

Upvotes: 3

Views: 2690

Answers (2)

Ryan Gross
Ryan Gross

Reputation: 6515

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.

Upvotes: 2

Aniket Inge
Aniket Inge

Reputation: 25723

To every process on a 32 bit computer, the memory addressable is 4GB, whether the computer actually has 4GB of actual RAM memory or not is unimportant.

For example, if your computer has 2GB of RAM, to the JVM, it looks like there is 4GB of memory. The other 2GB of memory comes from mapping the memory to a file in the HDD(this is the virtual memory).

If there are multiple processes running, to each process, it seems like it is the only process running on the computer, with 4GB of addressable memory to it. Since, JVM will not be the only process running, the memory manager in the operating system may decide to push the content of unused memory onto the Virtual Memory file. this is upto the OS to decide.

Since it is swapping to disk, the lag in performance will be noticable(unless it is a Solid State Hard drive)

Upvotes: 1

Related Questions