Java Runtime Data Areas, Memory Model, Garbage Collection, and Class Loading Overview
This article provides a comprehensive overview of Java Virtual Machine runtime data areas, the Java Memory Model, heap memory segmentation, garbage collection algorithms, HotSpot VM details, and class loading mechanisms, offering practical insights for performance tuning and system design.
Java Virtual Machine divides its managed memory into several runtime data areas, each with specific purposes and lifetimes, including the program counter, virtual machine stack, native method stack, method area, heap, and runtime constant pool.
Java虚拟机在执行Java程序的过程中会将其管理的内存划分为若干个不同的数据区域,这些区域有各自的用途、创建和销毁的时间,有些区域随虚拟机进程的启动而存在,有些区域则是依赖用户线程的启动和结束来建立和销毁。Java虚拟机所管理的内存包括以下几个运行时数据区域,如图:The program counter points to the current bytecode instruction per thread. The JVM stack holds stack frames with local variables, operand stack, dynamic linking, and return values. The native method stack supports native calls. The method area stores loaded class metadata, constants, static variables, and JIT‑compiled code.
A、是方法区的一部分<br/>B、存放编译期生成的各种字面量和符号引用<br/>C、<span style="font-weight: 700">Class</span>文件中除了存有类的版本、字段、方法、接口等描述信息,还有一项是常量池,存有这个类的 编译期生成的各种字面量和符号引用,这部分内容将在类加载后,存放到方法区的运行时常量池中。The heap is the largest memory region, shared by all threads, storing object instances and arrays, and is managed by garbage collectors (GC). It is divided into Young Generation (Eden, Survivor From/To) and Old Generation (Tenured), with the former using copying collection and the latter using mark‑compact.
思想:把堆分成新生代和老年代。(永久代指的是方法区)Java Memory Model (JMM) defines how threads interact via shared memory, specifying main memory and per‑thread working memory, and the actions (lock, unlock, read, load, use, assign, store, write) that ensure visibility and ordering.
Garbage collection determines object reachability using GC Roots (stack references, static fields, native references, etc.) and proceeds through reachability analysis and finalization before reclaiming memory. Common GC algorithms include Mark‑Sweep, Copying, Mark‑Compact, and generational collection.
A、初始标记:标记GC Root能直接引用的对象<br/>B、并发标记:利用多线程对每个GC Root对象进行tracing搜索,在堆中查找其下所有能关联到的对象。<br/>C、重新标记:为了修正并发标记期间,用户程序继续运作而导致标志产生变动的那一部分对象的标记记录。<br/>D、并发清除:利用多个线程对标记的对象进行清除When full GC occurs frequently, tools such as jstat -gccause, jmap -histo, and inspection of GC locker can help identify causes.
A、当JVM无法为一个新的对象分配内存的时候,越容易触发Minor GC。所以分配率越高,内存越来越少,越频繁执行Minor GC<br/>B、执行Minor GC操作的时候,不会影响到永久代(Tenured)。从永久代到年轻代的引用,被当成GC Roots,从年轻代到老年代的引用在标记阶段直接被忽略掉。HotSpot VM implements GC using OopMaps to locate GC Roots at safe points, and supports both pre‑emptive and cooperative thread interruption during GC.
A、抢占式中断:在GC发生时,首先把所有线程中断,如果发现有线程不在安全点上,就恢复线程,让它跑到安全点上。<br/>B、主动式中断:GC需要中断线程时,不直接对线程操作,仅仅设置一个标志,各个线程执行时主动去轮询这个标志,当发现中断标记为真就自己中断挂起。轮询标记的地方和安全点是重合的。Class loading follows the three‑phase process of loading, linking (verification, preparation, resolution), and initialization, using a parent‑delegation model to ensure security and consistency. Custom class loaders can load classes from unconventional sources or apply decryption.
Understanding these JVM internals enables effective performance tuning, such as adjusting heap sizes ( -Xmx, -Xms), generation ratios ( -XX:SurvivorRatio), and selecting appropriate GC algorithms ( -XX:+UseParallelGC, -XX:+UseConcMarkSweepGC).
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
