SoFunction
Updated on 2025-05-13

Detailed explanation of Java method call duration and performance of string operation

In software development, performance optimization is the key to ensuring that the application runs efficiently. As a widely used programming language, Java provides a variety of tools and methods to help developers identify and solve performance problems. Especially in distributed systems, cloud environments or high load scenarios, performance issues may directly affect user experience and system stability.

1. Identify performance bottlenecks using a Profiler

Performance issues are usually difficult to locate directly, and then a Profiler comes in handy.

Analyzers can help developers understand how the application is running, including key metrics such as method call frequency, execution time, and memory usage.

Common Java analyzers

  • VisualVM: Once part of Oracle JDK, it now exists independently as an open source project (VisualVM). It provides an intuitive interface to help developers monitor and analyze the performance of Java applications, including CPU usage, memory allocation, and threading activity.
  • Java Flight Recorder: Built-in in the JDK, used to record the runtime data of the application, and can be analyzed through Java Mission Control (Java Mission Control). It is especially suitable for analyzing performance issues in production environments.
  • Third-party analyzer: Such as YourKit and JProfiler, provide more detailed performance analysis functions, suitable for in-depth optimization of complex applications.

The role of the analyzer

Using analyzers, developers can quickly locate performance bottlenecks. For example, an analyzer can show which methods are called frequently or executed for too long, thereby helping developers optimize their code in a targeted manner. In addition, the analyzer can also reveal the memory allocation pattern and garbage collection behavior, providing a basis for memory optimization.

2. Manual timing method call

Sometimes, developers need to manually measure the execution time of a specific method to verify the optimization effect or understand the overhead of method calls.

Java provides()Methods can record the time before and after the method is called, thereby calculating the execution time of the method.

Manual timing example

Here is a simple example showing how to use it()Measurement method execution time:

public class TimeMethod {
    public static void main(String[] args) {
        long startTime = ();
        // Call the method that needs to be measured        someMethod();
        long endTime = ();
        ("Method execution time: " + (endTime - startTime) + " ms");
    }

    public static void someMethod() {
        // Method implementation        try {
            (1000); // Simulation method execution time        } catch (InterruptedException e) {
            ();
        }
    }
}

Pros and cons of manual timing

The manual timing method is simple and easy to use, suitable for quickly verifying the performance of a specific code segment. However, it cannot provide a comprehensive view like the parser, such as method call stack or memory allocation. Therefore, manual timing is often used for preliminary testing or in scenarios where the analyzer is not available.

3. Case Study: String Conjunction vs. Multiple Prints

In Java, string operations are one of the common operations, but different operations can have a significant impact on performance. Here is a classic case that compares the performance differences between string concatenation and multiple prints.

Test scenario

Consider a scenario where a large number of HTML tags are needed to be generated, containing strings in fixed formats. We designed two programs to compare performance:

  • StringPrintA: Use string concatenation to generate the complete string and print it.
  • StringPrintB: Through multiple calls()Print string fragments separately.

Here are the codes for the two programs:

  • StringPrintA (String concatenation)
public class StringPrintA {
    public static void main(String[] argv) {
        Object o = "Hello World";
        for (int i = 0; i < 100000; i++) {
            ("<p><b>" + () + "</b></p>");
        }
    }
}
  • StringPrintB (Print multiple times)
public class StringPrintB {
    public static void main(String[] argv) {
        Object o = "Hello World";
        for (int i = 0; i < 100000; i++) {
            ("<p><b>");
            (());
            ("</b></p>");
            ();
        }
    }
}

Test results

By running these two programs and measuring execution time, the following results were obtained (data from 2004, 2014 and 2024):

years StringPrintA (seconds) StringPrintB (seconds)
2004 17.23, 17.20 27.59, 27.60
2014 0.714, 0.525 1.091, 1.039
2024 0.146, 0.075 0.298, 0.282

Results Analysis

From the results, it can be seen that both in early Java versions and modern Java versions,StringPrintB(Print multiple times)StringPrintA(String concatenation) is about 1.5 times slower. The main reasons for this performance difference include:

  • Optimization of string concatenation:existStringPrintAIn, string concatenation (e.g."<p><b>" + () + "</b></p>") is usually optimized by the Java compiler and usedStringBuilderInternal implementation reduces the creation of temporary objects.
  • Overhead of multiple printing:existStringPrintBevery call()or()Both involve synchronous operations (to prevent multithreaded calls interleaved) and I/O operations, resulting in additional performance overhead.

Further optimization: Use StringBuilder

To further improve performance, it can be used explicitlyStringBuilderto build strings. The following is an optimized version (StringPrintAA):

public class StringPrintAA {
    public static void main(String[] argv) {
        Object o = "Hello World";
        for (int i = 0; i < 100000; i++) {
            StringBuilder sb = new StringBuilder();
            ("<p><b>").append(()).append("</b></p>");
            (());
        }
    }
}

The test results show thatStringPrintAAslightly better performance thanStringPrintA, because explicitly usedStringBuilderAvoid additional overhead that the compiler may introduce.

Conclusion: In performance-sensitive scenarios, it is recommended to use string concatenation orStringBuilder, instead of calling the print method multiple times. In addition, modern Java compilers optimize string concatenation+Operations are efficient enough in many cases, but for a lot of string operations in a loop, explicitly useStringBuilderStill a best practice.

4. The impact of garbage collection on performance

Garbage Collection (GC) is the core mechanism of Java memory management. It automatically recycles objects that are no longer used to prevent memory leakage. However, GC operation can cause performance fluctuations, especially in high load scenarios.

The basis of garbage recycling

The main task of GC is to identify and release objects that are no longer referenced and release memory for subsequent use. However, GC runs require pause of the application (called "pause time"), which may affect response time or throughput.

Modern garbage collector

In recent years, Java has introduced new garbage collectors, which significantly improves performance:

  • Z Garbage Collector (ZGC): Supports extremely low pause time (the goal is within 10ms), suitable for applications with high real-time requirements (ZGC)。
  • Shenandoah: Provides low latency and high throughput, supports concurrent GC operations, suitable for large-scale applications (Shenandoah)。

These modern GCs reduce pause time through concurrency and parallel technologies, making Java more suitable for low-latency and high-performance scenarios.

Optimize garbage collection

Developers can optimize GC performance by:

  • Choose the right GC: Choose GC with low latency (such as ZGC) or high throughput (such as G1) according to application requirements.
  • Monitor GC behavior: Use tools such as Java Mission Control orGC easyAnalyze GC logs and identify potential problems.
  • Reduce object allocation: Optimize code to reduce unnecessary object creation, thus reducing GC burden.

Latest progress

According to technical data for 2025, ZGC and Shenandoah continue to evolve, enabling more efficient memory management and lower pauses (Java Code Geeks). In addition, AI-assisted GC tuning tools are emerging to provide developers with smarter configuration suggestions (GC easy)。

Summarize

Performance optimization is an indispensable part of Java development. This article helps readers understand the key points of Java performance optimization by introducing the impact of analyzers, manual timing methods, string operation performance comparison and garbage collection.

In actual development, developers should choose appropriate optimization strategies based on specific circumstances and verify the optimization effect through analyzers and tests. At the same time, focusing on the latest advances in Java technology, such as the new garbage collector, can further improve the performance of applications.

Best Practices:

  • Test-driven optimization: Don't optimize the code based on guessing, always verify performance improvements through tests.
  • Using StringBuilder: When performing string operations in a loop, useStringBuilderReduce object creation.
  • Choose the right GC: Select a garbage collector with low latency or high throughput according to the application scenario.
  • Regular monitoring: Use the analyzer to check application performance regularly to ensure no new bottlenecks appear.

The above is personal experience. I hope you can give you a reference and I hope you can support me more.