Java from Basics to Mastery — Part 9
Understanding the roles of JVM memory, GC, and JIT
The articles written on this blog were created with the goal of teaching. Also, the information in the article may be incorrect, so please be careful.
Java code runs on top of the JVM and handles class loading, memory management, and execution optimization with the help of the runtime. Rather than memorizing the internal terminology, it is important to understand how this process affects performance and errors.
Class loading and bytecode
Source.java
↓ compiler
Source.class
↓ class loader
JVM execution
The class loader finds the required class and verifies that it is in a form that the JVM can use. If the class path or dependencies do not match, the class may not be found during execution even if compilation is successful.
stack and heap
Method call information and local variables are managed in the call frame of each thread. Objects that can be referenced from multiple places are created in the managed heap area.
void update() {
int damage = 10;
Monster monster = new Monster();
monster.takeDamage(damage);
}
damage is a primitive type value and the monster variable is an object reference. Actual placement and optimization are determined by the JVM implementation, so all object locations should not be judged by simple formulas.
Garbage Collector
Objects that can no longer be reached become subject to GC collection. Having GC does not mean there will never be memory leaks.
If a static collection or event listener continues to refer to an unused object, it remains reachable. External resources such as files and sockets should be closed directly without waiting for GC.
JIT optimization
The JVM can optimize code by observing frequently used code during execution and compiling it into machine language. Judging overall performance from just one short execution time can confuse preparation and optimization.
For performance issues, quota, CPU usage, and lock waits should be measured using a profiler before changing the heap size or GC options. JVM knowledge is the basis for interpreting the causes of measurement results, not for speculative micro-optimization.