Operations 8 min read

Diagnosing Java OOM, High CPU, Disk & Network Issues: A Practical Guide

This guide explains how to troubleshoot common online Java problems—including high CPU, OutOfMemoryError, disk I/O, and network latency—by using tools such as top, jps, jstack, jmap, MAT, df, and ping to pinpoint root causes like infinite loops, memory leaks, lock contention, and resource exhaustion.

Coder Life Journal
Coder Life Journal
Coder Life Journal
Diagnosing Java OOM, High CPU, Disk & Network Issues: A Practical Guide

Online Java incidents often involve CPU, memory, disk, and network problems, and these symptoms frequently overlap. For example, insufficient memory can trigger frequent GC, leading to high CPU usage and slow responses; high disk I/O can cause CPU waiting; and network latency can cause timeouts, retries, and bandwidth pressure.

CPU Diagnosis

High CPU usage manifests as response delays, timeouts, or thread blockage, often caused by business logic errors (e.g., infinite loops) or frequent GC. The following steps help locate the culprit:

1. Use <code>jps</code> or <code>ps -ef|grep <app_name></code> to find the process PID.
2. Run <code>top -H -p <pid></code> to identify the thread with the highest CPU usage, then convert its PID to hexadecimal with <code>printf '%x' <tid></code> to obtain the TID.
3. Execute <code>jstack <pid> | grep '0x<tid>' -C5</code> to view the stack trace of the high‑CPU thread.
4. Analyze the stack to see if a <code>RUNNABLE</code> thread is stuck in a loop, multiple <code>BLOCKED</code> threads are waiting on the same lock (lock contention), or threads form a circular wait (deadlock).
5. Optionally capture a full thread dump for broader analysis.
6. Generate a lock‑inclusive dump with <code>jstack -l <pid> > thread_dump.log</code>.
7. Count thread states: <code>cat thread_dump.log | grep "java.lang.Thread.State" | sort -nr | uniq -c</code>.
8. Typical findings include thread leaks (increasing thread count), lock contention, I/O waiting (<code>TIMED_WAITING</code>), or GC pressure from multiple GC threads.

Memory (OOM) Diagnosis

Memory anomalies such as OutOfMemoryError, frequent GC, or off‑heap memory exhaustion cause sudden crashes or slow responses. OOM types are categorized as follows:

Heap memory overflow : Object count exceeds heap limit or a leak exists. Error: java.lang.OutOfMemoryError: Java heap space. Fix: Resolve code issues first; otherwise increase -Xmx.

Metaspace overflow : Excessive dynamic class generation (e.g., CGLib proxies). Error: java.lang.OutOfMemoryError: Metaspace. Fix: Resolve code issues; optionally raise -XX:MaxMetaspaceSize.

Stack overflow : Infinite recursion. Error: java.lang.StackOverflowError. Fix: Resolve code; optionally adjust -Xss.

Direct memory overflow : Large ByteBuffer allocations not released. Error: java.lang.OutOfMemoryError: Direct buffer memory. Fix: Identify and fix direct‑memory leaks; can set -XX:MaxDirectMemorySize (typically ¼–½ of heap).

Thread count overflow : OS thread limit exceeded due to leaks or misconfiguration. Error:

java.lang.OutOfMemoryError: Unable to create new native thread

. Fix: Resolve code; reduce per‑thread stack size with -Xss if needed.

To locate OOM and StackOverflow issues, follow these steps:

1. Run <code>free -h</code> to view overall memory usage.
2. Export a heap dump: <code>jmap -dump:format=b,file=heap.hprof <pid></code>.
3. Open the dump with MAT, select <em>Leak Suspects</em> to get leak recommendations, and use the Histogram view to spot large objects.
4. In production, add <code>-XX:+HeapDumpOnOutOfMemoryError</code> to automatically save dumps on OOM.
5. Monitor GC impact on CPU and memory with <code>jstat -gcutil <pid> 1000 10</code>, watching metrics S, E, U, O, YGCT, YGC, FGC, FGCT.
6. Enable detailed GC logging in production via <code>-XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:/path/to/gc.log</code>.

Disk Diagnosis

Disk problems (insufficient space or high I/O) lead to functional failures, errors, or slow responses. Common commands include df -h for space and iostat for I/O pressure. Disk issues are usually secondary, so detailed analysis is omitted.

Network Diagnosis

Network latency, disconnections, or bandwidth shortages cause remote call timeouts and connection failures. Causes may include missing network policies or serving many images without CDN caching. Verify connectivity with ping or telnet, and check CDN usage by inspecting the Age header in browser developer tools.

The article concludes with an invitation for readers to comment and share experiences.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaperformancetroubleshootingCPUOOMMATjstack
Coder Life Journal
Written by

Coder Life Journal

An ordinary programmer sharing tech and life.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.