JVM tuning interview questions - parameter command topics

1. What are the JVM parameters?

1.1. Standard parameters

-version
-help
-server
-cp

image.png

1.2, -X parameter

-Xint     解释执行
-Xcomp    第一次使用就编译成本地代码
-Xmixed   混合模式,JVM自己来决定

image.png

1.3, -XX parameter

The most used parameter type, mainly used for JVM tuning and Debug

a.Boolean类型
格式:-XX:[+-]<name>            +或-表示启用或者禁用name属性
比如:-XX:+UseConcMarkSweepGC   表示启用CMS类型的垃圾回收器
	 -XX:+UseG1GC              表示启用G1类型的垃圾回收器
b.非Boolean类型
格式:-XX<name>=<value>表示name属性的值是value
比如:-XX:MaxGCPauseMillis=500   

1.4. Other parameters

This block is also equivalent to a -XX type parameter

-Xms1000M等价于-XX:InitialHeapSize=1000M
-Xmx1000M等价于-XX:MaxHeapSize=1000M
-Xss100等价于-XX:ThreadStackSize=100

1.5. View parameters

 java -XX:+PrintFlagsFinal -version > flags.txt

image.png
image.png

It is worth noting that "=" indicates the default value, and ":=" indicates the value modified by the user or JVM.
Generally, to set parameters, you can first check what the current parameters are, and then modify them

1.6. Common ways to set parameters

  • Settings in development tools such as IDEA, eclipse
  • When running the jar package: java -XX:+UseG1GC xxx.jar
  • Web containers such as tomcat can be set in scripts
  • Adjust the parameters of a java process in real time through jinfo (only the flags marked as manageable can be modified in real time)

1.7, the meaning of common parameters

parameter meaning illustrate
-XX:CICompilerCount=3 Maximum number of parallel compilations If the setting is greater than 1, although the compilation speed will increase, it will also affect system stability and increase the possibility of JVM crashes
-XX:InitialHeapSize=100M Initialize heap size Abbreviation-Xms100M
-XX:MaxHeapSize=100M maximum heap size Abbreviation-Xms100M
-XX:NewSize=20M Set the size of the young generation
-XX:MaxNewSize=50M young generation maximum size
-XX:OldSize=50M Set the old generation size
-XX:MetaspaceSize=50M Set method area size
-XX:MaxMetaspaceSize=50M method area maximum size
-XX:+UseParallelGC Use UseParallelGC New generation, throughput priority
-XX:+UseParallelOldGC Use UseParallelOldGC In the old generation, throughput is prioritized
-XX:+UseConcMarkSweepGC use CMS Old generation, pause time priority
-XX:+UseG1GC Use G1GC New generation, old generation, pause time priority
-XX: NewRatio Ratio of new to old generation For example, -XX:Ratio=4 means that the new generation: the old generation = 1:4, that is, the new generation occupies 1/5 of the entire heap memory
-XX:SurvivorRatio The ratio of the two S areas to the Eden area For example -XX:SurvivorRatio=8, that is (S0+S1):Eden=2:8, that is, one S accounts for 1/10 of the entire new generation
-XX:+HeapDumpOnOutOfMemoryError start heap memory overflow print When the JVM heap memory overflows, that is, OOM, a dump file is automatically generated
-XX:HeapDumpPath=heap.hprof Specifies the heap memory overflow print directory Indicates that a heap.hprof file is generated in the current directory
-XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -Xloggc:g1-gc.log Print out the GC log You can use different garbage collectors to compare and view the GC situation
-Xss128k Set the stack size for each thread The best experience value is 3000-5000
-XX:MaxTenuringThreshold=6 Increase the maximum threshold of the old generation The default value is 15
-XX:InitiatingHeapOccupancyPercent Heap memory usage ratio when starting concurrent GC cycle Garbage collectors such as G1 use it to trigger concurrent GC cycles based on the usage of the entire heap, not just the usage ratio of a certain generation of memory. A value of 0 means "always execute GC cycles". The default value is 45.
-XX:G1HeapWastePercent Percentage of wasted heap space allowed The default is 10%. If the reclaimable space of concurrent marking is less than 10%, MixedGC will not be triggered.
-XX:MaxGCPauseMillis=200ms G1 maximum pause time The pause time should not be too small, if it is too small, G1 will not be able to keep up with the speed of garbage generation. Eventually it degenerates into Full GC. Therefore, the tuning of this parameter is a continuous process, gradually adjusting to the best state.
-XX:ConcGCThreads=n The number of threads used by the concurrent garbage collector The default value varies depending on the platform the JVM is running on
-XX:G1MixedGCLiveThresholdPercent=65 Old locale occupancy threshold to include in mixed garbage collection cycles The default occupancy rate is 65%
-XX:G1MixedGCCountTarget=8 Sets the target number of mixed garbage collections to be performed on the old area whose survival data upper limit is G1MixedGCLIveThresholdPercent after the marking cycle is completed The default is 8 times of mixed garbage collection, and the goal of mixed garbage collection is to control it within this target number
-XX:G1OldCSetRegionThresholdPercent=1 When describing Mixed GC, Old Region is added to CSet By default, G1 only adds 10% of the Old Region to the CSet

2. What are the common commands of JVM?

2.1、jps

View the java process

image.png

2.2、jinfo

View and adjust JVM configuration parameters in real time

# 查看某个java进程属性的值
jinfo -flag MaxHeapSize PID 
jinfo -flag UseG1GC PID

image.png

# 参数只有被标记为manageable的flags可以被实时修改
jinfo -flag [+|-] PID
jinfo -flag <name>=<value> PID
# 查看曾经赋过值的一些参数
jinfo -flags PID

image.png

2.3、to stand

View virtual machine performance statistics

# 查看类装载信息 每1000毫秒输出一次,共输出10次
jstat -class PID 1000 10 

image.png

# 查看垃圾收集信息
jstat -gc PID 1000 10

image.png

2.4、jstack

View thread stack information

jstack PID

image.png

Troubleshoot deadlock cases

//运行主类
public class DeadLockDemo
{
    
    
    public static void main(String[] args)
    {
    
    
        DeadLock d1=new DeadLock(true);
        DeadLock d2=new DeadLock(false);
        Thread t1=new Thread(d1);
        Thread t2=new Thread(d2);
        t1.start();
        t2.start();
    }
}
//定义锁对象
class MyLock{
    
    
    public static Object obj1=new Object();
    public static Object obj2=new Object();
}
//死锁代码
class DeadLock implements Runnable{
    
    
    private boolean flag;
    DeadLock(boolean flag){
    
    
        this.flag=flag;
    }
    public void run() {
    
    
        if(flag) {
    
    
            while(true) {
    
    
                synchronized(MyLock.obj1) {
    
    
                    System.out.println(Thread.currentThread().getName()+"----if获得obj1锁");
                    synchronized(MyLock.obj2) {
    
    
                        System.out.println(Thread.currentThread().getName()+"----if获得obj2锁");
                    }
                }
            }
        }
        else {
    
    
            while(true){
    
    
                synchronized(MyLock.obj2) {
    
    
                    System.out.println(Thread.currentThread().getName()+"----否则获得obj2锁");
                    synchronized(MyLock.obj1) {
    
    
                        System.out.println(Thread.currentThread().getName()+"----否则获得obj1锁");

                    }
                }
            }
        }
    }
}

jstack analysis
Pull the printed information to the end and you can find 1 deadlock information.
image.png

2.5、jmap

# 打印出堆内存相关信息
jmap -heap PID

image.png

# 生成堆快照
jmap -dump:format=b,file=heap.hprof PID

image.png

Generally, during development, the following two sentences can be added to the JVM parameters, so that when the memory overflows, the file will be automatically dumped

-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=heap.hprof

3. Can you estimate the GC frequency?

Normally, we should make a memory estimate based on our system. We can test this in the test environment. At the beginning, we can set the memory to be larger, such as 4G. Of course, this can also be estimated based on the business system.

For example, to obtain a piece of data from the database occupies 128 bytes and needs to obtain 1000 pieces of data, then the size of the memory read at one time is ((128 B/1024 Kb/1024M) * 1000 = 0.122M, then our program may need to read concurrently Take, for example, read 100 times per second, then the memory usage is 0.122100 = 12.2M, if the heap memory is set to 1 G, then the size of the young generation is about 333M, then 333M*80%/12.2M =21.84s, that is to say Our program performs youngGC almost two to three times per minute. This allows us to have a rough estimate of the system.

4. How to solve memory overflow (OOM)?

There are generally two reasons for memory overflow:

  • Insufficient memory: in the case of large concurrency
  • Code problem: memory leak leads to memory overflow

4.1, large concurrency [seckill]

Cache, CDN, cluster + load balancing, current limiting, etc.

4.2. Memory leaks lead to memory overflow

  • jstack checks the thread status, whether there is deadlock or IO blocking, and finds the class name that may have problems or the process number with the longest waiting time.

    jstack PID
    
  • Check the use of heap memory, get the jvm.hprof file, and upload it to the specified tool for analysis, such as heaphero.io

    jmap -heap PID
    

Guess you like

Origin blog.csdn.net/qq_28314431/article/details/129418650