Understanding Java Heap Memory: Prevent OutOfMemory Errors and Optimize GC
This article explains Java heap memory concepts, garbage collection, common memory issues like leaks and OutOfMemoryError, and outlines the team's quality assurance framework—including left‑shift code review, right‑shift monitoring, and continuous delivery practices—to help prevent and resolve production alerts.
1. Background
Recently the R&D team has been actively handling alerts; after remediation the situation improved but some alerts remain, e.g., a production UMP alert showing an application’s heap memory usage at 90.18% exceeding the 85% threshold.
1.1 Java Heap Memory
Definition
Java heap memory is a part of JVM memory used for dynamic allocation of objects and arrays. It exists from JVM start to shutdown and its size can be adjusted with JVM parameters such as -Xms (initial heap size) and -Xmx (maximum heap size).
Characteristics
Dynamic allocation : objects can be allocated and freed at runtime.
Automatic management : the garbage collector (GC) automatically reclaims unreachable objects.
Global access : any part of the program can reference objects in the heap.
Generational collection : the heap is divided into generations (Young and Old) to improve GC performance.
Structure
The heap is typically divided into:
Young Generation : contains Eden and Survivor spaces (S0, S1). New objects are allocated in Eden; after several GC cycles surviving objects move to Survivor and eventually to Old Generation.
Old Generation : stores long‑living objects.
Permanent Generation / Metaspace : holds class metadata; since Java 8 the permanent generation was replaced by Metaspace, which resides outside the heap.
How Allocation Works
When an object is created with new, the JVM allocates space in the heap, e.g., new Person("John", 25) creates a Person instance.
public class Main {
public static void main(String[] args) {
// 创建一个对象
Person person = new Person("John", 25);
}
}
class Person {
String name;
int age;
//构造方法
Person(String name, int age) {
this.name = name;
this.age = age;
}
}Garbage Collection
GC automatically reclaims memory of objects that are no longer referenced. The process generally includes marking live objects, sweeping away dead ones, and optionally compacting the heap to reduce fragmentation.
Common Heap Problems
Memory leaks : objects remain referenced even though they are no longer needed, preventing GC.
OutOfMemoryError : occurs when the heap is exhausted and GC cannot free enough memory.
Performance impact : frequent GC cycles can degrade application performance, so memory usage and GC strategy should be optimized.
2. Quality Assurance
2.1 Existing Framework
2.2 Key Elements
Requirement Quality
PRD documents from product, design docs from development, and test cases from QA constitute the primary deliverables for requirements.
Process Quality
Requirement reviews, design reviews, and test case reviews align understanding among product, development, and testing.
Delivery Quality
Test teams issue acceptance reports; product performs requirement acceptance; before release product conducts business acceptance, feeding back issues for further optimization.
2.3 Quality Control
Environment governance is handled by a dedicated team. Diff&CR belongs to left‑shift testing, while monitoring alerts and quality dashboards belong to right‑shift testing.
2.4 Delivery Process
Continuous Iteration
Two‑week sprint cycles fill the backlog each iteration.
Continuous Integration
Based on the corporate Coding platform for code repository and release management; a custom release system is used due to special requirements.
Continuous Operation
A dedicated team manages operations.
Continuous Measurement
Online issue‑handling groups coordinate on‑call support; feedback is collected via insurance‑line voice, user satisfaction surveys, etc.
2.5 Quality Culture
Quality and efficiency are core team values: test thoroughly and test quickly.
3. Improvement Measures
3.1 Left‑Shift Testing (Diff&CR)
Traditional diff&CR required manual repository access and was time‑consuming. Leveraging large‑model‑assisted code review improves efficiency; AI‑driven CR can uncover hidden risks and enrich bad‑case testing.
3.2 Right‑Shift Monitoring & Alerts
Initial alert analysis showed incomplete information; optimizing alert messages makes them more understandable for non‑technical stakeholders.
4. Future Plan
Under the current quality framework, we will continue to explore, discover, and optimize to ensure smoother business operation.
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.
JD Cloud Developers
JD Cloud Developers (Developer of JD Technology) is a JD Technology Group platform offering technical sharing and communication for AI, cloud computing, IoT and related developers. It publishes JD product technical information, industry content, and tech event news. Embrace technology and partner with developers to envision the future.
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.
