Memory overflow and Memory leakage, memory overflow and Memory leakage

Source: Internet
Author: User

Memory overflow and Memory leakage, memory overflow and Memory leakage

Basic Concepts

Memory overflow out of memory means that the program does not have enough memory space for use when applying for memory. For example, the system only stores integer space, however, you have applied for long storage, that is, memory overflow. For example, 20 bytes of memory space are defined, but 21 bytes of data are written. In layman's terms, the memory is insufficient and the current program cannot be supported. (When memory overflow occurs, the program cannot proceed and is forcibly terminated..)

Memory leakage memory leak refers to the failure of the program to release the applied memory space after applying for memory. The memory leakage hazard can be ignored once, but the memory leakage accumulation result is very serious, no matter how much memory, sooner or later. (In case of Memory leakage, the available memory will gradually decrease, thus reducing the performance .)

Memory leak will eventually cause out of memory!

Memory overflow means that the amount of memory you need to allocate exceeds what the system can offer. If the system cannot meet your needs, it will cause an overflow.

Memory leakage means that you apply to the system to allocate memory for use (new), but do not return it after use (delete ), as a result, the memory you applied for cannot be accessed by yourself (maybe you have lost its address), and the system cannot allocate it to the required program again. A plate can only contain four fruits in a variety of ways. If you have five fruits, you cannot eat them if you drop them. This is overflow! For example, stack overflow occurs when the stack is full. Stack Overflow also occurs when the stack is empty. That is, the allocated memory is insufficient to put down the data sequence, which is called memory overflow.

 

Java memory overflow

1. There is an endless loop in the program, or there are too many loops, and an instance of too many repeated objects is generated;

2. There is an object reference and it is not cleared after use. As a result, the Java Virtual Machine cannot be recycled;

3. During a single operation, a large amount of data is loaded in the memory;

In principle, in JAVA, the possibility of memory overflow is not very high due to its automatic garbage collection mechanism ].

 

Memory leakage type

Memory leakage can be classified as follows:
1. Frequent Memory leakage. Code with Memory leakage will be executed multiple times, resulting in a memory leak each time it is executed.
2. Occasional Memory leakage. Memory leakage occurs only in certain environments or operations. The frequency and frequency are relative. For a specific environment, unexpected events may become frequent. Therefore, the test environment and test method are crucial for detecting memory leaks.
3. One-time memory leakage. The code with Memory leakage is executed only once, or due to algorithm defects, there will always be only one piece of Memory leakage. For example, if the class constructor allocates memory but does not release the memory in the destructor, the memory leakage only occurs once.
4. Implicit Memory leakage. The program continuously allocates memory during the running process, but it does not release the memory until it ends. Strictly speaking, there is no memory leakage because the program releases all requested memory. However, if a server program needs to run for several days, weeks, or even months, failing to release the memory in time may eventually exhaust all the system memory. Therefore, we call this type of Memory leakage an implicit memory leak.

 

Memory leakage

Memory leakage may reduce the number of available memory to reduce computer performance. In the end, in the worst case, too much available memory is allocated, causing all or some devices to stop working normally or applications to crash.
Memory leakage may not be serious and can even be detected by conventional means. In modern operating systems, an application is released upon termination of a regular internal program used by the application. This indicates that memory leakage in a transient running application will not cause serious consequences.
In the following cases,Memory leakage causes serious consequences:
* When the program runs, it ignores the issue and consumes more and more memory (for example, background tasks on the server, especially background tasks in embedded systems) as time passes, these tasks may be ignored for many years after running );
* New memory is frequently allocated, for example, when a computer game or animated video screen is displayed;
* The program can request unreleased memory (such as shared memory), or even terminate the program;
* Leakage occurs inside the operating system;
* Leakage occurs in key system drivers;
* Memory is very limited, for example, in an embedded system or a portable device;
* When running on an operating system (such as AmigaOS) where the memory is not automatically released at the time of termination, and once lost, it can only be restored by restarting.

 

Summary

From the perspective of user programs, memory leakage does not cause any harm. As a general user, the memory leakage does not exist. The real danger is the accumulation of Memory leakage, which will eventually consume all the memory of the system. From this point of view, one-time memory leakage is not harmful because it will not accumulate, while implicit memory leakage is very harmful, because it is more difficult to detect than frequent and occasional memory leaks.

JAVA's garbage collection mechanism demonstrates JAVA's robustness and security, rational code design, and effective evaluation of memory usage. Basically, the above problems will not occur.

 

  Thank you: Thank you for your patience!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.