Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What if you use e.printStackTrace () to output log in java to make the system crash?

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces how to use e.printStackTrace () output log in java to let the system crash. The article is very detailed and has certain reference value. Interested friends must finish reading it!

Talk about a mistake you may often make! Use e.printStackTrace () to output how the log crashes the system!

From the point of view of the self-annotation of the method printStackTrace, the method outputs the stack trace information of the print exception. Because, from the day we learned Hello World, teachers use printStackTrace to output error logs, resulting in many people have been mistakenly using it and still use it!

Seriously, printStackTrace could cause our system to crash. Because, when e.printStackTrace () prints an exception to the console, it will store the error stack string into the string pool memory space. If the space at this time is relatively small and unusually large, this memory space may suddenly be full, and some threads that produce strings in this memory space will not be fully produced, and there will be no space, resulting in a large number of threads producing strings in the middle of the output, all waiting here. Waiting for each other and waiting for free memory will eventually throw OOM, causing the whole application to hang up.

In this case, if you use java jvisualvm to check memory usage, you will find that the rightmost non-heap area in the following figure, the string constant pool, is full!

Then check the thread information.

You will find that a large number of threads are stuck in the location of the abnormal output. View the source code through the specific line number information.

It is indeed the pot of e.printStackTrace ().

So how to solve it?

1. Improve the code quality and solve the problem from the source. First solve why an exception is thrown.

2. Increase memory, increase non-heap memory, and increase the memory of string constant pool. 3. It is prohibited to use e.printStackTrace () to output logs. 4. Improve the fault tolerance of the system.

In addition, e.printStackTrace () outputs the log to the console, which is powerless if I want to output the log to a file or to a third-party server. And if you use a lot of e.printStackTrace () in your system, then the time and cost of changing it will be too high!

In addition, many people like to use System.out.println () to output logs. I don't recommend it either. Last year I wrote an article about the dangers of System.out.println. I wonder if you still remember? It's 9102 years old, and you don't know the harm of System.out.println! These are all the contents of the article "what to do if the system crashes with e.printStackTrace () output log in java". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report