🔥 面试 | Java 相关



JVM

JVM 基础与内存模型

JVM 的内存结构是怎样的?(堆、方法区、程序计数器、虚拟机栈、本地方法栈)

The JVM memory consists of Heap, Method Area(Meta Space), VM Stack, Program Counter Register and Native Method Stack.

堆和栈的区别?对象一定分配在堆上吗?

The stack stores method calls and local variables, while the heap stores object instances. With escape analysis, some objects can be allocated on the stack.

Escape analysis means Object references from some methods are not returned, or not used externally

Java 内存模型(JMM)的作用是什么?它如何保证线程安全?

JMM defines the visibility, atomicity, and ordering rules for multithreaded variable access, solving reordering issues from compiler and CPU.

The thread safety can be accompilshed by synchronization keywords(synchronized, volatile, final), CAS operations and locking mechanisms.

什么是逃逸分析?它会带来哪些优化(如标量替换、栈上分配)?

Escape analysis checks if an object escapes a method/thread. If not, it can be stack-allocated or scalar replaced, reducing heap allocation.

强引用、软引用、弱引用、虚引用的区别?各自应用场景?

The reference type of the object is related to determine whether an object is dead.

Strong Reference: never collected by GC.
Soft Reference: colleceted only when memory is low, often used for caches.
Weak Reference: Collected in the next GC.
Phantom Reference: Used for tracking object finalization, no impact on lifecycle.

类加载机制

JVM 类加载过程包括哪几个阶段?(加载、链接、初始化)

It includes loading, verification, preparation, resolution and initialization.

类加载器(ClassLoader)的双亲委派模型是如何工作的?为什么要这样设计?

In the parent delegation model, a class loader delegates the loading request to its parent. if the parent cannot find the class, the current loader will try. This prevents duplicate loading and protects core classes.

自定义类加载器的使用场景有哪些?

什么是热加载(HotSwap),JVM 如何支持类的动态加载?

JVM supports dynamic class loading through Classloader and allows hot replacement via JVMTI or instrumentation API.

垃圾回收

JVM 中对象什么时候会被判定为可回收?

JVM uses GC Roots reachability analysis. If an object is not reachable from GC Roots, it is considered garbage. GC Roots include references from the stack, static variables, and native methods.

引用计数法和可达性分析法的优缺点?

Reference Counting is simple and real-time but cannot handle circular references.

Reachability Analysis solves circular references and is used by modern JVMs, but it requires Stop-The-World which causes pause when traversing the entire object gragh from GC Roots.

Minor GC、Major GC、Full GC 的区别?

Minor GC cleans the Young Generation(fast),
Major GC cleans the Old Generation(slow),
and Full GC cleans the entire heap and method area.

常见的垃圾收集器有哪些?(Serial、ParNew、CMS、G1、ZGC、Shenandoah)

Common GC algorithms include Serial, Parnew, CMS, G1, ZGC and Shenandoah.

CMS 收集器的优缺点是什么?为什么 CMS 会有“浮动垃圾”?

CMS is concurrent and low-latency but may cause fragmentation and floating garbage.

G1 收集器的原理是什么?为什么说它是面向延迟的收集器?

G1 divides the heap into reginos and collects based on priority, making latenncy more predictable.

如何判断一个对象在 GC Roots 可达性分析中存活?

The object will survive If it is related with any object in the reference chain of GC Roots.

Stop-The-World(STW)是什么?为什么会发生?

STW occurs when all application threads pause during GC or at a safepoint.

A safepoint is a JVM stop point where all threads must pause, typically used during GC or profiling.

性能优化与调优

JVM 常用的性能调优参数有哪些?(-Xmx、-Xms、-XX:+UseG1GC 等)

-Xms / -Xmx: Initial and max heap size

-Xss: Thread stack size

-XX:MetaspaceSize: MetaSpace size

-XX:+UseG1GC: Enable G1 collector

-XX:+PrintGCDetails: Print GC logs

如何排查 JVM 内存泄漏问题?常用工具有哪些?(jmap、jstack、jconsole、VisualVM、Arthas)

Tools like jmap, jstack, visualVM and Arthas can help analyze memory and threads. GC logs are also crucial for identifying issues.

发生 OOM(OutOfMemoryError)时如何定位和解决?

Java heap space
GC overhead limit exceeded
Metaspace
Direct buffer memory
Unable to create new native thread

JVM 调优时常见的监控指标有哪些?(GC 次数、延迟、堆内存使用率、线程数)

During JVM tuning, I mainly monitor GC count and pause time, heap and old generation usage, thread numbers and states, and CPU utilization.

In addtion, I also check application-level latency indicators, such as P99 response time, to see if GC affects performance.

生产环境中遇到 GC 频繁、响应变慢,你会如何排查?

First,I will check GC logs to see whether frequent GCs are Minor or Full.

Then I use tools like jstat, jmap and jstack to analyze heap memory usage and thread states.

If it’s caused by insufficient memory, I adjust heap size or GC parameters;

If it’s a code issue, such as cache not being cleard or memory leak, I analyze heap dumps and fix the code.

Finally, I may switch to a more suitable garbage collector, such as G1.

JVM 是如何实现线程切换和线程安全的?

JVM relies on OS-level scheduling. During a context switch, it saves and restores thread states, including registers and. stack pointers.

JVM ensures thread satety via the JMM(java memory model), synchronization keywords(synchronized, volatile), CAS operations and locking mechanisms.

JVM 中有哪些即时编译优化?(如方法内联、逃逸分析、锁消除)

JIT optimizations include method inlining, escape analysis, lock elimination, loop unrolling, and dead code elimination.

内存泄漏导致 JVM 内存爆满 pod 一直重启排查。

it will cause memory leaks such as repeatedly creating new ClassLoader instances with Class.forName, storing reflected objects in static collections without release, or frequently fetching Method/Field objects without caching.

Thus, the Metaspace will be exhaused. This process shows the increasing memory usage until hitting the limit and then the pod restart.

We dumped the heap with jmap and analyzed it using MAT, and found a large number of relection-related objects being strongly referenced in static collectinos. This prevented GC from reclaiming them.

THe root cause was that our code created reflectino objects but never released them. We fixed it by using weak references for caching or explicitly clearing references after use.