Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do the -Xms and -Xmx flags reserve the machine's resources?

Tags:

java

jvm

I know that the -Xms flag of JVM process is to allow the JVM process to use a specific amount of memory to initialize its process. And in regard to performance of a Java application, it is often recommended to set the same values to both -Xms and -Xmx when starting the application, like -Xms2048M -Xmx2048M.

I'm curious whether the -Xms and -Xmx flags mean that the JVM process makes a reservation for the specific amount of memory to prevent other processes in the same machine from using it.

Is this right?

like image 553
ParkCheolu Avatar asked Apr 09 '17 03:04

ParkCheolu


People also ask

What are reservations in Google Cloud?

A reservation ensures that capacity is held in a specific zone even if the reserved VMs are not running. Reservations apply only to Compute Engine, Cloud Dataproc, and Google Kubernetes Engine VM usage.

Does GCP have reserved instances?

You can reserve up to 1,000 VM instances per reservation. Reservations apply only to Compute Engine, Dataproc, and Google Kubernetes Engine VM usage. Reservations don't apply to the following resources: f1-micro and g1-small machine types.

How do I know if my Azure VM is reserved instance?

As an Enterprise Agreement administrator, you can now see Azure reserved instances purchased in your enrollment by going to the Azure portal. To view reservation transactions, go to Cost Management + Billing > Billing accounts.


2 Answers

Xmx merely reserves virtual address space. Xms actually allocates (commits) it but does not necessarily prefault it.

How operating systems respond to allocations varies.

Windows does allow you to reserve very large chunks of address space (Xmx) but will not allow overcommit (Xms). The limit is defined by swap + physical. The exception are large pages (which need to be enabled with a group policy setting), which will limit it by physical ram.

Linux behavior is more complicated, it depends on the vm.overcommit_memory and related sysctls and various flags passed to the mmap syscall, which to some extent can be controlled by JVM configuration flags. The behavior can range from a) Xms can exceed total ram + swap to b) Xmx is capped by available physical ram.

like image 81
the8472 Avatar answered Sep 25 '22 15:09

the8472


Short answer: Depends on the OS, though it's definitely a NO in all popular operating systems.

I'll take the example of Linux's memory allocation terminology here.

-Xms and -Xmx specify the minimum and maximum size of JVM heap. These sizes reflect VIRTUAL MEMORY allocations which can be physical mapped to pages in RAM called the RESIDENT SIZE of the process at any time.

When the JVM starts, it'll allocate -Xms amount of virtual memory. This can be mapped to resident memory (physical memory) once you dynamically create more objects on heap. This operation will not require JVM requesting any new allocation from the OS, but will increase you RAM utilization, because those virtual pages will now actually have corresponding physical memory allocation too. However, once your process tries to create more objects on heap after consuming all its Xms allocation on RAM, it has to request the OS for more virtual memory from the OS, which may/may not also be mapped to physical memory later depending on when you need it. The limit for this is your -Xmx allocation.

Note that this is all possible because the memory in linux is shared. So, even if a process allocates memory beforehand, what it gets is virtual memory which is just an addressable contiguous fictional allocation that may or may not be mapped to real physical pages depending on the demand. Read this answer for a short description of how memory management works in popular operating systems. Here is a much detailed (slightly outdated but very useful) information on how Linux's memory management works.

Also note that, these flags only affect heap sizes. The resident memory size that you will see will be larger than the current JVM heap size. More specifically, the memory consumed by a JVM is equals to its HEAP SIZE plus DIRECT MEMORY which reflects things coming from method stacks, native buffer allocations etc.

like image 33
Ashu Pachauri Avatar answered Sep 25 '22 15:09

Ashu Pachauri