Java memory and Windows

Something kind of meaning is noticed today, but I cannot explain the semantics exactly.

Basically I am creating a simple old java method main

and it has an endless while loop in it. In this loop, I create some strings and put them in a HashMap. All I really want is a process that starts and accumulates its memory usage 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 starts with -Xms512m -Xmx512m

.

Once started, I can use procexp.exe

to view my java process. The bits I am trying to understand are virtual memory and physical memory. The virtual memory (private bytes) seems to match the requested jvm ie 512MB size. From the profile of the process over time, I am guessing that the physical memory (working set) is the actual memory used by the process as it crawls over time as my process generates more String values ​​and populates the map. This is always well below 512MB.

So my question is, why does Java use virtual memory?

Isn't that all in RAM? Physical memory?

Since I am using virtual memory, does that mean it is replacing the disk?

Is this really bad for performance?

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

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

+3


source to share


2 answers


Why does Java use virtual memory?

All user applications interact with OS memory using virtual memory. Backing up back to physical RAM / disk (via the page table) is OS dependent. Since you started the JVM with -Xms512m, it will ask for 500 MB of virtual memory when it starts, but the OS decides not to reserve 500 MB of physical memory for this, because it will never be used.

Isn't that all in RAM? Physical memory?

If your computer is using a high percentage (> 75% or so), all of its physical RAM and a JVM-based application recently accessed some of its data, the JVM will be in RAM.

Since I am using virtual memory, does that mean it is replacing the disk?

Not. See above.

Is this really bad for performance?



Paging can be bad for performance in an application that has its data paged out to disk. Windows will try to minimize this impact in several ways:

  • Virtual memory pages that are sent to disk are the least recently used
  • There are several optimizations that are designed to get a page out of disk if Windows detects that it will be in use anytime soon (i.e. if you start iterating through the keys on your map, then Windows will try to discard pages containing the following block of keys before reaching iteration)

Also, as I said, you are most likely not pushing to disk.

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

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

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

+2


source


For each process on a 32-bit computer, the address memory is 4 GB, regardless of whether the computer actually has 4 GB of RAM or not.

For example, if your computer has 2GB of RAM, then for the JVM it is like 4GB of memory. The other 2 GB of memory is related to the memory-to-file mapping on the hard disk (this is virtual memory).



If there are multiple processes running in each process, it appears that it is the only process running on the computer with 4 GB of addressable memory. Since the JVM will not be the only process, the memory manager in the operating system might decide to push the contents of unused memory into a virtual memory file. it depends on the OS to decide.

As it switches to disk, the performance lag will be noticeable (unless it is a solid state hard drive)

0


source







All Articles