What is Out Of Memory (OOM) and Memory Leak (Memory Leak)

1. Memory overflow: (Out Of Memory---OOM)


 The system can no longer allocate the space you need. For example, the system now only has 1G of space, but you want 2G of space. This is called a memory overflow
example: a plate can only hold 4 fruits after exhausting various methods. You loaded 5 of them and they fell to the ground and couldn't eat them. This is overflow.

For example, when the stack is full, pushing into the stack will inevitably cause space overflow, called overflow, and when the stack is empty, pushing back into the stack will also cause space overflow, called underflow. That is, the allocated memory is not enough to put down the data item sequence, which is called memory overflow. To put it bluntly, I can't bear that much, so report an error.

 

2. Memory Leak: (Memory Leak)

The object pointed to by the strong reference will not be recycled, which may lead to memory leaks. The virtual machine would rather throw OOM than recycle the object pointed to. This
means that when you use resources, you open up a space for him, and when you run out I forgot to release the resources. At this time, the memory is still occupied. It doesn’t matter once, but too many memory leaks will cause memory overflow.

(About the differences and connections between strong, weak, and soft references can be viewed here: click to enter )


Example: You apply to the system to allocate memory for use (new), but you do not return it (delete) after using it. As a result, you can no longer access the memory you applied for (maybe you lost its address) ), and the system cannot assign it to the required program again. It is equivalent to renting a cabinet with a key. After you lock the cabinet after storing things, you lose the key or fail to return the key. The result is that the cabinet will not be available for anyone to use and cannot be garbage. The collector reclaimed because it could not find any information about him.

For example, memory leaks may occur when we use ThreadLocal. What is ThreadLocal? You can click here to understand: Click to enter


Generally speaking, the memory leak refers to the leak of heap memory. Heap memory refers to the program allocated from the heap. After the random size is used up, the released memory must be displayed. There is a free function in C++/C to release memory. Java There is a garbage collection mechanism in it without the programmer manually calling the release.
If this memory is not released, it can no longer be used, which is called a memory leak

Guess you like

Origin blog.csdn.net/qq_27471405/article/details/108889184