编程知识 cdmana.com

Analyze the mystery of Java's death

http://xjjdog.cn Yes 200+ The original articles are classified in detail , Reading more smoothly , Welcome to collect .

original : Taste of little sister ( WeChat official account ID:xjjdog), Welcome to share , Reprint please keep the source . Any reprint without reservation of this statement is plagiarism .

Java The process suddenly disappeared , There's no information about them in the log , They just evaporate out of thin air . journal 、OOM Some configuration parameters of , It doesn't work at all .

Don't panic . The process has no soul . One restart, It's going to make these programs come alive again.

The question is what restart Problems that can't be solved , And Murphy's law, which works behind the scenes .

Who killed the beloved Java process ?

Don't be too heartless , Before I die , At least let the process make a few last words . This section will analyze several common Java The mystery of process disappearance , Let you calm down and watch the flowers bloom and fall .

They could be :

  • Judged by the operating system
  • Executed the God function , Buried by a teammate
  • Using the wrong way to start
  • Log system configuration error

1. Judged by the operating system

The following problems have been encountered by more than one partner : my java The process is gone , Nothing left , Direct evaporation is gone .

why? It's because of too much love , Too many people ?

This is a very interesting and technical problem .

perform dmesg command , Probably see your process crash message lying there .

In order to see when it happened , We habitually add parameters T

dmesg -T

Obviously, the operating system doesn't like your progress , to Kill 了 .

This phenomenon , and Linux It's about memory management .

because Linux The system uses virtual memory allocation ,JVM Of Code , library , Pile up and Stack All of them will consume memory , But the requested memory , As long as it's not really access too , It doesn't count , Because there is no physical page assigned to it .

As memory is used more and more . The first protective wall is SWAP; When SWAP It's almost used , Will try to release cache; When both resources are exhausted , There's a killer .oom killer It will jump out when the system runs out of memory , Selectively kill some processes to free a little memory .

So at this time our Java process , It's the operating system “ Take the initiative ” The end of ,JVM There's not even a chance to say goodbye . This information , It can only be found in the operating system log .

To solve this problem , First, don't be too greedy . For example, a total of 8GB Machine , You put the whole 7.5GB Assigned to the JVM. When the operating system runs out of memory , Yours JVM It could be oom-killer Prey of .

however , With the following command , It allows the process to avoid being tried .

echo -17 > /proc/[PID]/oom_adj

This is because ,oom_adj file , It's that the process is oom killer The weight of killing , Generally between [-17,15] Between . The higher the weight , Means more likely to be oom killer Choose .

Once you do that , Yours Java The process is the privileged class , You can ignore the rules .

2. Executed the God function

xjjdog The evaluation of this function is : You know it better than you do , Maybe you don't know it .

Don't look at me for this function . It's about you ,System.exit.

This function is very dangerous , It will force the termination of our application , And nothing will be left behind . You should scan your code , Make sure that such logic doesn't exist .

believe me , You don't need to use procedural judgment to end the process immediately , Business systems in particular don't have . If there is , That big probability is unreasonable . Unless you put Java When the script uses .

This function , It's a very advanced burrow skill , Especially in Android In applications like this . Application crash , You can't analyze any reason , Even if you do ShutdownHook.

buyaoluanyongwo

Use exit function , Be kind .

Of course, we are not at a loss about this . Here's the code , Can stop exit Implementation , Domineering is extraordinary . God's hand , Break it back .

import java.security.Permission;

public class S {
    private static class ExitTrappedException extends SecurityException {
    }
    private static void forbidSystemExitCall() {
        final SecurityManager securityManager = new SecurityManager() {
            public void checkPermission(Permission permission) {
                if (permission.getName().startsWith("exitVM")) {
                    throw new ExitTrappedException();
                }
            }
        };
        System.setSecurityManager(securityManager);
    }
    private static void enableSystemExitCall() {
        System.setSecurityManager(null);
    }
    public static void main(String[] args) {
        forbidSystemExitCall();
        try {
            System.exit(0);
        }catch (Exception ex){
            ex.printStackTrace();
        }
        System.out.println(" thank you xjjjdog,  I can still carry out ");
    }
}

If you do everything you can , Can't find the reason for the abnormal termination , Try hanging this code . It could be lifesaving .

3. Wrong way to start

Let's talk about the most Elementary is the most common It's also a situation that often happens , Can cause accidental death of the application : That's right. Java The wrong way to start a program .

Many students are right Linux Not very familiar with , Use XShell After landing , Call the following command to start .

java com.cn.AA &

This student is a little bit conscious , In the end, I used & Number , To expect the process to run in the background . But unfortunately , In many cases , With XShell Tab Closing of the page , Or wait timeout , hinder Java The process stops with it , It's confusing .

The right way to start , Is the use of nohup keyword , Or get stuck in other, longer-lived processes ( such as docker).

nohup java com.cn.AA &

therefore , When you log in to the terminal tty When , Be sure to figure out who the parent process is currently executing . You may be the ancestor of all the processes to be run next .

4. Log configuration error

If none of the above reasons are , The big probability is that the configuration of the log framework in your project is wrong .Java Logs in various frameworks , There are many ways of configuration , carelessly , You'll step in the pit . Even if you're using SpringBoot, Also because of the problem of relying on packages , Cause startup problems .

Log configuration error + Abnormal situation , Of course, nothing will be left behind .

Use the following command , You can transfer the dependency tree to log Analysis in the file .

mvn dependency:tree > dep.log

If it is SpringBoot project , It can be given to main Class with some code .

public static void main(String[] args) {
  try {
   SpringApplication.run(LinkpowerDtulockApplication.class, args);
  } catch (Exception e) {
   System.out.println(e);
  }
}

What's wrong with this , You can find out earlier .

End

in addition , There are also some strange reasons . For example, the disk is full , Not enough handles , These situations are very hidden , You need to control the details of the system .

Process, this quiet way of death , It usually brings more difficulties to our problem investigation .

Usually , When we shut down the service , Will use “kill -15”, instead of “kill -9”, So that the service can breathe before it dies . But it doesn't always work , Because there's no chance for the program to make a last word , There is a higher level of presence that prevents it .Java The process is dead , We can only look for other means .

Author's brief introduction : Taste of little sister (xjjdog), An official account that does not allow programmers to take turns. . Focus on infrastructure and Linux. Ten year structure , 10 billion traffic per day , Discuss the world of high concurrency with you , Give you a different taste . My personal wechat xjjdog0, Welcome to add friends , Further communication .

This article is from WeChat official account. - Taste of little sister (xjjdog) , author : Miss's dog

The source and reprint of the original text are detailed in the text , If there is any infringement , Please contact the yunjia_community@tencent.com Delete .

Original publication time : 2020-12-17

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

版权声明
本文为[xjjdog]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224122136499d.html

Scroll to Top