How JVM and Java Memory Management Works — Summary
Java Virtual Machine Architecture and Java Memory Management Summary
What is JVM?
- JVM (Java Virtual Machine) is a part of JRE (Java Runtime Environment).
JRE is a set of components to create and run a Java application.
- Java code is compiled into Java byte code.
JVM converts Java byte code into machine language.
- JVM provides a runtime environment for Java code / apps (an instance).
- JVM is also responsible for allocating memory space.
JIT (Just In Time Compiler)
- compiles parts of byte code with similar functionality to reduce time for compilation
- compiler here ->JVM’s instructions->a specific CPU’s instruction
JNI (Java Native Interface)
- framework provides interface to communicate with another app written in another language (C, C++, Assembly, etc.)
- Java uses JNI to send output to the console or interact with OS libs.
Java Memory Management
- a fully qualified name of a type (for ex. String)
- the type’s modifiers
- type’s direct superclass name
- type’s super interfaces
- actual objects (new keyword) -> their references are at stack
- heap becomes full -> garbage collector (gc)
- Strong Reference (not eligible)
- Weak Reference (when gc runs)
- Soft Reference (when low on memory)
- Phantom Reference (whenever gc wants)
Mark and Sweep Algorithm
- root object
- heap is traversed, find gap between live objects
- non-reachable objects are collected.