# JVM Internals

## JVM (Java Virtual Machine)

The JVM is an abstract machine that runs compiled Java code. It provides a layer of abstraction between the operating system and the Java code, allowing Java programs to be executed on any platform. Here are some important points to keep in mind about the JVM:

* **Class Loader** - Loads Java classes into memory at runtime
* **Execution Engine** - Executes the compiled Java code
* **Garbage Collector** - Manages memory allocation and deallocation
* **Java Native Interface (JNI)** - Allows Java code to call and be called by native applications and libraries
* **Java Development Kit (JDK)** - Includes the JVM as well as the tools needed for Java development

## Garbage Collection

Garbage collection is the process of automatically freeing up memory that is no longer being used by a program. The JVM includes a garbage collector that automatically manages memory allocation and deallocation. Here are some key points about garbage collection:

* **Heap** - The area of memory where objects are allocated
* **Generational Garbage Collection** - The garbage collector divides the heap into generations and collects objects based on their age and reachability
* **Young Generation** - The area of the heap where new objects are allocated
* **Old Generation** - The area of the heap where long-lived objects are allocated
* **PermGen/Metaspace** - The area of the heap where metadata about classes is stored. In Java 8, PermGen was replaced with Metaspace which is a non-heap memory.
* **Garbage Collection Algorithms** - The JVM provides several garbage collection algorithms, including:
  * **Serial Collector** - Single-threaded, compacting collector that is suitable for small applications or low-concurrency environments
  * **Parallel Collector** - Multi-threaded, non-compacting collector that is suitable for medium to large applications with high throughput
  * **CMS (Concurrent Mark and Sweep) Collector** - Multi-threaded, concurrent collector that is suitable for large applications with low pause time requirements
  * **G1 (Garbage First) Collector** - Multi-threaded, region-based collector that is suitable for large applications with high throughput and low pause time requirements.
