title: Understanding JVM Memory Allocation and Java Out of Memory: Heap Space
body: <p>I'm looking into really understanding how memory allocation works in the JVM.
  I'm writing an application in which I'm getting Out of Memory: Heap Space exceptions.</p>
  
  <p>I understand that I can pass in VM arguments such as Xms and Xmx to up the heap space that the JVM allocates for the running process. This is one possible solution to the problem, or I can inspect my code for memory leaks and fix the issue there.</p>
  
  <p>My questions are:</p>
  
  <p>1) How does the JVM actually allocate memory for itself? How does this relate to how the OS communicates available memory to the JVM? Or more generally, how does memory allocation for any process actually work?</p>
  
  <p>2) How does virtual memory come into play? Let's say you have a system with 32GB of physical memory and you allocate all 32GB to your Java process. Let's say that your process actually consumes all 32GB of memory, how can we enforce the process to use virtual memory instead of running into OOM exceptions?</p>
  
  <p>Thanks.</p>