introduction
In Java development, exception handling is an important means to ensure the robustness of the program. However, when the same exception is frequently thrown, the log may be overwhelmed by a large number of repeated stack information, affecting the efficiency of problem detection. How does JVM control the number of printing times of exception stack? What are the differences between different JDK versions? How to customize restrictions? This article will explore these issues in depth and verify them in combination with code examples.
1. Background for exception stack printing
1.1 The role of exception stack
The exception stack (Stack Trace) records the call chain when an exception occurs, helping developers quickly locate problems. For example:
try { throw new RuntimeException("Test Error"); } catch (RuntimeException e) { (); // Print stack}
Output:
: Test Error at (:5)
1.2 Problem of duplicate exceptions
If an exception is thrown multiple times in a loop or high-frequency call, the log may be filled with a lot of repeated stack information:
for (int i = 0; i < 1000; i++) { try { throw new RuntimeException("Repeated Error"); } catch (RuntimeException e) { (); } }
This can cause log files to bloat and even affect performance.
2. JVM restrictions on duplicate exception stack
In order to avoid excessive repeated stack information, JVM introduces a mechanism for limiting the number of exception stack printing times.
2.1 Default behavior of JDK 1.8
- Default limit: 100 times
The same exception (same type and message) is printed up to 100 times. The full stack is full, and only a brief message is output after it exceeds it. - Control parameters:
(JDK 1.8)
Verification code
public class JDK8ExceptionLimitTest { public static void main(String[] args) { for (int i = 1; i <= 150; i++) { try { throw new RuntimeException("Test Exception - " + i); } catch (RuntimeException e) { (); } } } }
Output observation:
- First 100 times: Full stack information.
- After the 101st time: Similar
[Repeated 100 times, original stack trace at ...]
A brief information.
2.2 Default behavior of JDK 9+
- Default limit: 2 times (stricter, less log redundancy)
- Control parameters:
(JDK 9+)
Verification code
public class JDK9ExceptionLimitTest { public static void main(String[] args) { ("", "2"); for (int i = 1; i <= 5; i++) { try { throw new RuntimeException("JDK9 Test Exception - " + i); } catch (RuntimeException e) { (); } } } }
Output observation:
- First 2 times: Full stack.
- After the 3rd time: Brief information.
3. How to customize exception stack printing restrictions?
3.1 Modify JVM parameters
- JDK 1.8:
java -=10 MyApp
- JDK 9+:
java -=10 MyApp
3.2 Dynamic adjustment during runtime
public class CustomExceptionLimit { public static void main(String[] args) { // JDK 1.8 ("", "5"); // JDK 9+ // ("", "5"); for (int i = 1; i <= 10; i++) { try { throw new RuntimeException("Custom Limit Test - " + i); } catch (RuntimeException e) { (); } } } }
Output:
- First 5 times: Full stack.
- After the 6th time: Brief information.
4. Optimization solution for logging framework
Although the JVM provides a restriction mechanism, it is recommended to use log frameworks (such as Log4j, SLF4J) to manage exception logs in actual development, which provide more flexible duplicate log suppression strategies.
4.1 Duplicate log suppression for Log4j 2
<Configuration> <Loggers> <Logger name="" level="error"> <Filters> <!-- Suppress the same exception to continuously print beyond3Second-rate --> <DuplicateFilter timeout="5" level="warn"/> </Filters> </Logger> </Loggers> </Configuration>
4.2 SLF4J + Logback Configuration
<configuration> <appender name="STDOUT" class=""> <filter class=""> <allowedRepetitions>2</allowedRepetitions> <!-- Repeat allowed2Second-rate --> </filter> </appender> </configuration>
5. Summary and best practices
project | JDK 1.8 | JDK 9+ |
---|---|---|
Default limit | 100 times | 2 times |
Control parameters |
|
|
Recommended adjustments | Adjust according to business needs (e.g.-=20 ) |
Can be relaxed appropriately (such as-=5 ) |
Best Practice Recommendations
- The recommended production environment is limited to 5-20 times, to avoid excessive log size but retain sufficient debugging information.
- In combination with logging frameworks (such as Log4j, Logback) to manage exception logs to provide more refined control.
- Monitor high-frequency exceptions and optimize the code to avoid repeated exception thrown.
Conclusion
Java's exception stack printing restriction mechanism effectively reduces log redundancy, but different JDK versions behave differently. Developers should combine JVM parameters and log framework to reasonably manage exception logs and improve system maintainability. I hope this article can help you better understand and optimize Java exception logs!
The above is a detailed explanation of the usage of the Java exception stack printing number limit mechanism. For more information on the Java stack printing number limit, please pay attention to my other related articles!