当前位置:网站首页>The new colleague wrote a few pieces of code, broke the system, and was blasted by the boss!
The new colleague wrote a few pieces of code, broke the system, and was blasted by the boss!
2022-07-29 07:45:00 【Ma Xiaoxiao】
Java The program is based on GC Of , At the beginning of startup , I applied for a sufficient amount of memory pool , Plus JIT And the real-time optimization of the compiler , It's no faster than using C++ Slow language writing .Java Language is both reflective and observable , Plus JFR This artifact , When a problem occurs, it is easier to find its root than binary files .
Recently in to see RCA(Root Cause Analysis) Things that are , Accidentally found out yCrash Such a thing . It's a few small pieces of problem code, which is very typical , We can take a little look at , Let's see Java Several common crash scenarios for applications .
1. Heap space overflow
OOM It's usually caused by a memory leak , Performance in GC In the Journal , In general, it's GC It's getting longer , And the effect of each recovery is very general .GC after , The actual usage of heap memory is on the rise .
The following code is an endless loop , Keep going to HashMap Richards data , because myMap Belong to GCRoots, Never released , So the end result is OOM.
import java.util.HashMap;
public class OOMDemo {
static HashMap<Object, Object> myMap = new HashMap<>();
public static void start() throws Exception {
while (true) {
myMap.put("key" + counter, "Large stringgggggggggggggggggggggggggggg"
+ "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"
+ "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"
+ "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"
+ "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"
+ "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"
+ "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"
+ "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"
+ "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"
+ "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"
+ "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"
+ "ggggggggggggggggggggggggggggggggggggggggggggggggggggg"
+ counter);
++counter;
}
}
}
2. Memory leak
Memory leaks and memory overflows are the same thing , The difference is its meaning .
Memory overflow may be due to excessive requests , Or the consequences of real business needs , The memory overflow is unknown 、 Beyond expectations OOM situation .
We can use the same code above to achieve this .
In reality , Memory leaks are usually very hidden , Need help Mat Wait for tools to find the root cause .jmap、pmap And so on are common tools .
such as , If you forget to rewrite the object hashCode and equals Method , There will be a memory leak .
//leak example : created by xjjdog 2022
import java.util.HashMap;
import java.util.Map;
public class HashMapLeakDemo {
public static class Key {
String title;
public Key(String title) {
this.title = title;
}
}
public static void main(String[] args) {
Map<Key, Integer> map = new HashMap<>();
map.put(new Key("1"), 1);
map.put(new Key("2"), 2);
map.put(new Key("3"), 2);
Integer integer = map.get(new Key("2"));
System.out.println(integer);
}
}
3.CPU soaring
Direct a dead cycle , You can put the CPU Dry to death .
public class CPUSpikeDemo {
public static void start() {
new CPUSpikerThread().start();
new CPUSpikerThread().start();
new CPUSpikerThread().start();
new CPUSpikerThread().start();
new CPUSpikerThread().start();
new CPUSpikerThread().start();
System.out.println("6 threads launched!");
}
}
public class CPUSpikerThread extends Thread {
@Override
public void run() {
while (true) {
// Just looping infinitely
}
}
}
To get the problem code, you can usually use the following method .
(1) Use top command , Found using CPU Most of a process , Record its pid. Use Shift + P Shortcut keys can be pressed CPU The usage rate of the .(2) Again using top command , Add -H Parameters , View the... Used in a process CPU The largest number of threads , Record the thread's ID.(3) Use printf function , Will be decimal tid Convert to hex .(4) Use jstack command , see Java Thread stack of process .(5) Use less Command to view the generated file , And find the hexadecimal conversion just now tid, Find the thread context where the problem occurred .
4. Thread leak
Thread resources are expensive . If you keep creating threads , System resources will soon be exhausted . The following code keeps creating threads , If there is more pressure to request at the same time , Most can kill the host .
public class ThreadLeakDemo {
public static void start() {
while (true) {
new ForeverThread().start();
}
}
}
public class ForeverThread extends Thread {
@Override
public void run() {
// Put the thread to sleep forever, so they don't die.
while (true) {
try {
// Sleeping for 10 minutes repeatedly
Thread.sleep(10 * 60 * 1000);
} catch (Exception e) {}
}
}
}
This is violence , This is similar to creating a thread for each request , Or the consequences of creating a thread pool are the same .xjjdog There are also two related thread leakage articles .
- Strongly opposed to the use of Spring Encapsulated multithreaded class !
- Fatal failure ! Blew up investors !
java.lang.OutOfMemoryError: unable to create new native thread
5. Deadlock
Deadlock codes generally do not occur , But once it happens, it's still very serious , Related businesses may not be able to run .
public class DeadLockDemo {
public static void start() {
new ThreadA().start();
new ThreadB().start();
}
}
public class ThreadA extends Thread {
@Override
public void run() {
CoolObject.method1();
}
}
public class ThreadB extends Thread {
@Override
public void run() {
HotObject.method2();
}
}
public class CoolObject {
public static synchronized void method1() {
try {
// Sleep for 10 seconds
Thread.sleep(10 * 1000);
} catch (Exception e) {}
HotObject.method2();
}
}
public class HotObject {
public static synchronized void method2() {
try {
// Sleep for 10 seconds
Thread.sleep(10 * 1000);
} catch (Exception e) {}
CoolObject.method1();
}
}
Deadlock is a serious situation ,jstack Will prompt with obvious information . Of course , About threads dump, There are also some online analysis tools available . such as fastthread, But you also need to understand the meaning of these situations .

6. Stack overflow
Stack overflow does not cause JVM Process death , harm “ Relatively small ”. The following is a simple code to simulate stack overflow , Just call recursively .
public class StackOverflowDemo {
public void start() {
start();
}
}
adopt -Xss Parameter can set the size of the virtual machine stack . For example, the following command is to set the stack size to 128K.
-Xss128K
If this happens frequently in your application , Try increasing this value . But it is usually caused by program errors , You'd better check your code .
7.Blocked Threads
BLOCKED Is a more serious thread state , When the processing time of the back-end service is very long , The requested thread will enter the waiting state . At this time through jstack To get the stack , You will find that the thread is blocked . It blocks the acquisition of locks (wating to lock)
public class BlockedAppDemo {
public static void start() {
for (int counter = 0; counter < 10; ++counter) {
// Launch 10 threads.
new AppThread().start();
}
}
}
public class AppThread extends Thread {
@Override
public void run() {
AppObject.getSomething();
}
}
public class AppObject {
public static synchronized void getSomething() {
while (true) {
try {
Thread.sleep(10 * 60 * 1000);
} catch (Exception e) {}
}
}
}
Once this happens frequently , It proves that your program is too slow . If CPU There is a surplus of resources , You can try to increase the number of threads requested , such as tomcat Is the maximum number of threads .
边栏推荐
- Chaos and future of domestic digital collections
- The smallest positive number that a subset of an array cannot accumulate
- 监听页面滚动位置定位底部按钮(包含页面初始化定位不对鼠标滑动生效的解决方案)
- MySQL 45 讲 | 07 行锁功过:怎么减少行锁对性能的影响?
- Docker's latest super detailed tutorial - docker creates, runs, and mounts MySQL
- @JsonSerialize注解的使用
- Credit card shopping points
- FLink CDC 的mysql connector中,mysql的字段是varbinary, 官方
- MapReduce steps of each stage
- QT connects two qslite databases and reports an error qsqlquery:: exec: database not open
猜你喜欢

NFT 的 10 种实际用途

Segger's hardware anomaly analysis

Jianmu continuous integration platform v2.5.2 release

Jiamusi Market Supervision Bureau carried out special food safety network training on epidemic and insect prevention

Android interview question | how to write a good and fast log library?

Can the subset of the array accumulate K

Halcon installation and testing in vs2017, DLL configuration in vs2017

207.课程表
![【暑期每日一题】洛谷 P6461 [COCI2006-2007#5] TRIK](/img/bf/c0e03f1bf477730f0b3661b3256d1d.png)
【暑期每日一题】洛谷 P6461 [COCI2006-2007#5] TRIK

Realize the effect of changing some colors of a paragraph of text
随机推荐
10 practical uses of NFT
LANDSCAPE
蓝桥杯A组选数异或
NFT 的 10 种实际用途
The new generation of public chain attacks the "Impossible Triangle"
[MySQL] - [subquery]
【暑期每日一题】洛谷 P1601 A+B Problem(高精)
NLP introduction + practice: Chapter 5: using the API in pytorch to realize linear regression
@Use of jsonserialize annotation
Getting started with JDBC
LANDSCAPE
【暑期每日一题】洛谷 P6320 [COCI2006-2007#4] SIBICE
cs61abc分享会(六)程序的输入输出详解 - 标准输入输出,文件,设备,EOF,命令行参数
flutter只要是数据,都会判空的
RoBERTa:A Robustly Optimized BERT Pretraining Approach
【FPGA教程案例42】图像案例2——通过verilog实现图像二值化处理,通过MATLAB进行辅助验证
佳木斯市场监管局开展防疫防虫害专题食品安全网络培训
My entrepreneurial neighbors
Up sampling deconvolution operation
mysql 使用 DATE_FORMAT(date,'%Y-%m')