When you ask for Linux commands, instead of listing the commands, say what problem you were solving with the command.

How is the OOM scenario reflected inside the JVM

SOF (Java. Lang. StackOverflowError) :

While causes method stack overflow, recursive call too deep causes stack overflow.

The array inside the function is too large, causing stack overflow.

OOM (Java. Lang. OutOfMemoryError) :

1. Java Heap space:

The heap memory is insufficient to hold newly created objects, the heap memory usage reaches the maximum memory limit, and memory leaks accumulate. For example, cache storage in memory. We fully store users in the database in advance and put them in Map for easy lookup. But now there is a requirement to pull down the user name immediately to display an obscure user name. Then each level of key will correspond to many user names, all of which need a separate Map storage, which may burst the memory and result in OOM. So we can use key+Set to filter out redundant user names at each level and reduce the number of objects.

Solution: Increase heap memory space

2.GC overhead limit exceeded:

Executors. NewFixedThreadPool provide default task queue unbounded thread pool, so the queue can fast extrusion, explode memory lead to OOM.

3.Direct buffer memory:

NIO ByteBuffer. AllocateDirect (capability) methods: using the local memory outside the JVM heap memory data copies. No JVM is used so GC is not triggered, objects are not collected, and over time local memory is used up, resulting in an OutOfMemoryError.

Solution: Manually release buffer and increase local memory

4. Unable to create new native thread:

Executors. NewCachedThreadPool thread pool, it is the maximum number of threads without limit, and it defaults to using SynchronousQueue will no storage space of the blocking queue, also means that as long as there is a request arrival will need to have a thread to execute, Creating an infinite number of threads causes OOM.

Solution: Reduce the number of threads and modify the upper limit of server threads

5.Metaspace:

The method area holds class information, constant pools, and static variables. This exception is reported when the metaclass is full

Memory leaks and memory overflows

Memory leakage refers to the waste of space caused by unused objects that are not reclaimed in time.

New subString() calls new String(), using the original String. However, if the subString uses only a small part of the String and the String is no longer used, the String is referenced by the subString and cannot be reclaimed, causing a memory leak

Memory overflow occurs OOM, garbage collection speed can not keep pace with scene object speed.

JVM parameters and commands

What do -xms, -xmx, and -xss stand for? Why do we have these parameters

-xms: specifies the size of the memory applied for when the program starts, that is, the size of the Java heap required when the process is created. When the initial memory is insufficient, it will be automatically expanded.

-Xmx: specifies the maximum memory size that can be occupied by a program when it is running.

-xss: specifies the stack size per thread (heap memory setting too large: addressing overhead increases, adding load to the JVM.

64 GB machine, JVM parameters how to configure:

The JVM’s initial -xMS defaults to 1/64 of physical memory, and -xmx defaults to 1/4 of physical memory. When the default free heap is less than 40%, the JVM increases the heap until -xmx; When free heap memory is greater than 70%, the JVM reduces the heap until -xms. So servers typically set -xms to be equal to -xmx to avoid having to adjust the heap after each GC. 32-bit systems have 4 gigabytes of controllable memory, so this is limited to 2 gigabytes to 3 gigabytes.

Jmap-heap PID = jMAP-heap pid = jMAP-heap PID = jMAP-heap PID

What parameters can be set for the JVM

-xms specifies the initial heap size, -xms specifies the maximum heap size, and -xss specifies the stack size for each thread

(2) – XX: + Use… GC: Specifies the type of GC to use.

③ Enable G1: -xx :UseG1GC

④ Set the maximum GC pause time (G1 expected pause time) : -xx :MaxGCPauseMills

⑤ Direct memory size: -xx :MaxDirectMemorySize

⑥ Print GC logs: see the following

Java parameters for troubleshooting performance problems

(1) Top: Displays the CPU usage and memory usage of each process in real time.

② top-HP PID: Displays the CPU usage of a specific thread.

③vmstat: a monitoring tool that can specify sampling periods and times. It can be used to check the number of context switches

④jstat: View heap memory information and GC information. Jstat -gc displays gc heap information, and jstat -gcutil displays GC information. It prints the usage of each cell and the number of GC counts.

⑤jstack: View the stack information of the thread.

⑥ JMAP: Output heap memory object information

To view GC information:

① jstat-gcutil pid {interval} : displays 100% occupancy and GC times of each element.

② -xx :+PrintGCTimeStamps -xx :+PrintGCDetails -xloggc :/log/ heaptest. log: dump running GC logs.

Troubleshoot problems

CPU 100% processing process:

① Run the top command to find the PROCESS PID that occupies the highest CPU usage

② Run the top-hp PID command to locate the tid of the thread that occupies the highest CPU usage

(3) through the jstack pid | grep dar {30} – A thread stack information, according to the information on the stack can be positioning problems in the project.

Check OOM memory overflow:

1) configure JVM startup parameters – XX: HeapDumpOnOutOfMemoryError and – XX: HeapDumpPath, dump the JVM OOM occurs will automatically generate log

(2) using jmap – dump: the format = b, the file = D: \ test \ heap 6956 export hprof dump log (process no. : netstat ano | find the process of “8080” to view the current applications.)

3. You can use the JDK’s jVisualVM to import dump snapshots and view them.

Checking for Memory leaks

Memory leaks can cause frequent Full GC, so you can use jstat-gcutil to view GC information.

Memory leaks can cause memory overruns, so see above.

Linux command

To view all running a Java program: ps – aux | grep Java

Awk: awk ‘{print 0}’

Look at the penultimate column, look at lines 10-20

tail -n 2

head -20 a.txt | tail -11

Head -n 100 displays the first 100 lines, and tail -n 100 displays the last lines starting at 100.

Top: displays the resource usage of each process in real time, such as CPU usage and AVERAGE CPU load.

CPU usage: busy CPU usage per unit time

Average CPU load: average number of processes that are running or hibernated but cannot be interrupted in the system per unit time.

How to view ports: netstat -anp

Chmod: r4 stands for read, w2 stands for write, and x1 stands for execute

Linux looks for a matching file name in a directory

find . -name ‘xxx’

Grep -rnl ‘XXX’ recursive search, no line number, only output file name

Linux looks for a filename in a directory that contains a string

The find. | xargs grep – ri ‘XXX’ -l, -i case-insensitive, -l output file name

Find the line in the file that contains the XXX string

sed -n ‘/xxx/p’ a.txt

More paging query (space down, CTRL +B up one screen)