Diagnosing and Optimizing Server CPU Spikes Caused by Excessive DateTime Object Creation in Java

This article explains how to identify and resolve server CPU spikes caused by excessive DateTime object creation in a Java application, detailing step‑by‑step Linux command usage, JStack and JProfiler analysis, and a refactoring solution that replaces DateTime comparisons with string comparisons to reduce GC pressure.

Snowball Engineer Team
Snowball Engineer Team
Snowball Engineer Team
Diagnosing and Optimizing Server CPU Spikes Caused by Excessive DateTime Object Creation in Java

Introduction

Developers who have dealt with online issues often encounter server CPU spikes and frequent GC events, which manifest as slow system performance and numerous alerts. This article focuses on diagnosing server CPU spikes and provides a systematic approach to locate the problematic code and propose a solution.

Problem Discovery

During the execution of the "maximum drawdown" algorithm for combination calculations, a noticeable CPU spike occurs each time the task runs.

Locating the Issue

1. Find the Java process PID: ps -ef | grep java 2. View all threads of the process: top -Hp PID 3. Convert the high‑CPU thread ID to hexadecimal:

printf '%x
' ID

4. Use jstack to inspect the thread stack: jstack PID | grep [threadHex] -A 30 5. Use JProfiler to examine CPU and memory usage, confirming that the thread consumes significant resources.

JProfiler reveals a large number of DateTime() object instances being created.

Problem Solution

The hotspot is the method V3PlanNavIndicatorCalcUtils.calMaxDrawDownRate(), which creates many DateTime() objects to sort and compare date strings (e.g., 20210818). The optimization replaces DateTime comparisons with direct string comparisons, eliminating the heavy object allocation.

Graphical Comparison

1. CPU usage comparison.

2. Physical machine load comparison.

3. G1 Young GC frequency comparison.

Summary

The article demonstrates a complete workflow for locating and solving an online performance issue using Linux commands, jstack, and JProfiler. The root cause was excessive DateTime() object creation, which increased memory usage and triggered frequent Young GC cycles.

Additional Note

The engineering team is hiring Java engineers, DevOps engineers, test developers, and algorithm engineers; interested readers can refer to the original post for details.

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.

JavaPerformanceOptimizationLinuxCPUgcprofiling
Snowball Engineer Team
Written by

Snowball Engineer Team

Proactivity, efficiency, professionalism, and empathy are the core values of the Snowball Engineer Team; curiosity, passion, and sharing of technology drive their continuous progress.

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.