1. Memory overflow
Out of memory is a colloquial term for running out of memory, such as when you apply for a large object and there is no room for it in the heap.
2. Memory leaks
Memory Leak usually refers to a waste of dynamically allocated Memory in a program that is not released or cannot be released, resulting in a slow program or a system crash.
3. Eight cases of memory leaks
Java has an automatic garbage collection mechanism that automatically reclaims objects that are no longer in use. How to judge whether an object can be reclaimed is mainly by reference counter method and reachable analysis algorithm, whose essence is to judge whether an object is referenced. But there are some exceptions in the code that often lead to memory leaks.
Points 1, 2, and 3 above are easy to understand. The main points are 4, 5, and 6
4. Inner classes hold outer classes
If an object of an external class instance returns an object of an inner class instance, the inner class object is long referred to. Even if the external class is not referenced, it will not be collected by the garbage collector, causing a memory leak
5. Change the hash
When an object is stored in a HashSet, it is not allowed to modify the fields involved in the calculation of the Hash value of the object. Otherwise, the modified Hash value of the object is inconsistent with the original stored Hash value. In this way, the result returned by retrieving the object with the contains method cannot be found, resulting in the failure to release and memory leak.
6. Expired references
The first common source of memory leaks is the presence of expired references. This is when we usually write code accidentally may cause a memory leak, if the usual version of frequent release may not be able to find.
/ * * *@author boren
* @date 2021/07/22 * /
public class MyStack {
/** * Define array */
private Object[] elements;
/** * Define size */
private int size = 0;
/** * The default capacity is 16 */
private static final int DEFAULT_CAPACITY = 16;
public MyStack() {
elements = new Object[DEFAULT_CAPACITY];
}
public void push(Object object) {
ensureCapacity();
elements[size++] = object;
}
public Object pop() {
if (size == 0)
throw new EmptyStackException();
return elements[--size];
}
private void ensureCapacity() {
if (elements.length == size) {
elements = Arrays.copyOf(elements, 2 * size + 1); }}}Copy the code
Above is a hand-written stack data structure, there are two methods in and out of the stack, seems to be no problem, but we have a lot of in and out of the stack, after the pop, the popup objects will not be collected, because the stack still retains the reference to these objects, the stale reference.
Solution:
There are also classic scenarios where ThreadLocal causes memory leaks, which are often asked about, because of these expired references.
4. Memory overflow scenario
There are two main types of memory overflows: OutOfMemoryError and StackOverflowError. Here are 10 common scenarios for overflows.
4.1 Java Heap Memory Overflows
When there is a Java. Lang. OutOfMemoryError: Java heap space anomaly, is the heap memory overflow.
Problem description
- The JVM memory set is too small, the memory space required by the object is too large, and the object is created out of memory
- There are limits to the amount of traffic or data spikes that the application itself can handle. When QPS surge can cause Java.lang.OutOfMemoryError
import java.util.List;
/ * * *@author boren
* @date 2021/07/22 * /
public class HeadOomError {
public static void main(String[] args) {
List<Byte[]> list = new ArrayList<>();
int i = 0;
while(true) {
list.add(new Byte[8*1024*1024]);
System.out.println("count is "+ (++i)); }}}Copy the code
You can see that memory overflow is very fast, which can also happen in the process of code development, such as writing a job to deal with batch data, you may make this error.
The solution
- The parameters -XMS and -xMX can be adjusted appropriately to achieve the optimal value by means of pressure measurement
- Try to avoid large objects, such as when we operate files or read database data, we need to consider the amount of data read at one time, and our system processing speed.
4.2 Java heap memory Leak
The end result of the memory leak eventually results in an OutOfMemoryError
package com.demo;
import java.util.HashMap;
import java.util.Map;
public class MemoryLeakOomError {
static class Key {
private Integer id;
public Key(Integer id) {
this.id = id;
}
@Override
public int hashCode() {
returnid.hashCode(); }}public static void main(String[] args) {
Map<Key, String> map = new HashMap<>();
while(true) {
for (int i = 0; i < 1000; i++) {
if (map.containsKey(new Key(i))) {
map.put(new Key(i), "number:" + i);
}
}
}
}
}
Copy the code
4.3 Memory Overflow due to Garbage Return Timeout
Question:
When the application is run out of all the available memory, the GC overhead restrictions than the error, and GC often failed to remove it, then it will lead to Java. Lang. OutOfMemoryError. An error is triggered when the JVM spends a large amount of time performing GC with little benefit, and when the entire GC process exceeds the limit (the default JVM configes GC for more than 98% of the time and reclaims heap memory for less than 2%).
3. Solutions
To reduce the object life cycle, garbage collection should be as fast as possible.
4.4 Metaspace Memory Overflow
1. Problem description
Dimension of overflow, the system will be thrown. Java lang. OutOfMemoryError: Metaspace. The reason for this exception is that the system has a lot of code or reference to a lot of third-party packages or through dynamic code generation class loading methods, resulting in a large memory footprint of the meta space.
4.5 Direct Memory Overflow
1. Problem description
In using ByteBuffer allocateDirect () is used, a lot of javaNIO (like netty) is encapsulated in the framework of to other methods, the problems thrown when the Java. Lang. OutOfMemoryError: The Direct buffer memory is abnormal.
A similar problem occurs if you use the allocateDirect method of the ByteBuffer directly or indirectly instead of using clear
4.6 Stack Memory Overflow
1. Problem description
When a thread executes a Java method, the JVM creates a new stack frame and pushes it to the top of the stack. The new stack frame becomes the current stack frame, which is used to store parameters, local variables, intermediate instructions, and other data when the method executes.
This is a common problem when we write our own recursive code.
3. Solutions
If the program does have recursion calls, when the stack overflow occurs, you can increase the -xSS size to solve the stack memory overflow problem. Recursive calls prevent an infinite loop that would otherwise overflow the stack memory.
4.7 Memory Overflow during Local Thread Creation
1. Problem description
Thread only occupy basic heap memory area, also is the area of this error shows that besides the heap, unable to thread a piece of memory, this is either the memory itself is not enough, or heap space set too big, lead to the rest of memory is running out, and because the thread itself takes up memory, so is not enough
4.8 The memory is out of the swap Area
1. Problem description
During Java application startup, you can limit the memory required by specifying -xmx and other similar startup parameters. When the total memory requested by the JVM is greater than the physical memory available, the operating system begins to convert the content from memory to hard disk.
Generally, the JVM throws an Out of Swap Space error, which indicates that the application failed to allocate memory to the JVM Native Heap and is running Out of memory. The error message contains the size (in bytes) of the failed allocation and the reason for the failed request.
4.9 Memory overflow due to Array Limit
1. Problem description
‘Requested array size exceeds VM limit’ Java has a limit on the maximum number of arrays that can be allocated by applications, which varies depending on the platform, but typically ranges between 100 and 2.1 million elements.
4.10 Memory Overflow of the System Kill Process
1. Problem overview
Before describing the problem, a little familiarity with operating systems: Operating systems are built on the concept of processes that work within the kernel, with a very special process called “Out of Memory Killer.” When the kernel detects that the system is running out of memory, OOM Killer is activated to check who is currently using the most memory and kill the process.
Common Out of memory:Kill process or sacrifice Child error is triggered when available virtual virtual memory (including swap space) is consumed to the point where the entire operating system is at risk. In this case, OOM Killer selects the “Rogue Process” and kills it.
Reference documentation
Crazy Maker JAVA Interview