The difference between memory overflow and memory leak and how to avoid memory overflow

Source: Internet
Author: User

Memory overflow out of memory, which means that the program does not have enough memory space for it to use when applying for memory, and it appears out of the memory; For example, an integer is applied, but the number that has long to save is the memory overflow.

Memory leak memory leak, refers to the program in the application of memory, can not release the requested memory space, a memory leakage hazard can be ignored, but the memory leak accumulation consequences are very serious, no matter how much memory, sooner or later will be taken up by the light.

Memory leak will eventually lead out of the memory.

Memory overflow is that you ask to allocate more memory than the system can give you, the system does not meet the requirements, resulting in overflow.

A memory leak is when you apply to the system to allocate memory for use (new), but after the use is not returned (delete), the result of the piece of memory you apply to you can no longer access (perhaps you have lost its address), and the system can not assign it to the required program. A dish can only hold 4 fruits in every way, and you have 5, so you can't eat it on the ground. This is the overflow. For example, stack, stack when you do into the stack must produce space overflow, called overflow, stack empty when the stack also produces space overflow, called underflow. Is that the allocated memory is not enough to drop the sequence of data items, called a memory overflow.

In the manner in which they occur, memory leaks can be categorized into 4 categories:

1. Frequent memory leaks. The code that has a memory leak is executed multiple times, causing a memory leak each time it is executed.
2. Accidental memory leaks. The code that occurs with a memory leak only occurs under certain circumstances or procedures. The frequent and incidental are relative. For a given environment, the occasional may become a frequent occurrence. So test environments and test methods are critical to detecting memory leaks.
3. Disposable memory leak. The code that has a memory leak is executed only once, or because of an algorithm flaw, there will always be a single and one memory leak. For example, allocating memory in the constructor of a class does not release that memory in the destructor, so a memory leak occurs only once.
4. An implicit memory leak. The program keeps allocating memory while it is running, but it does not release memory until the end. Strictly speaking, there is no memory leak, because the final program frees up all of the requested memory. But for a server program, it can take days, weeks, or months to run out of memory, which may result in the eventual exhaustion of all of the system's memory. So, we call this kind of memory leak as an implicit memory leak.

From the user's point of view of the program, the memory leak itself will not produce any harm, as a general user, do not feel the existence of memory leaks. What really harms is the accumulation of memory leaks, which ultimately consumes all the memory of the system. In this sense, a one-time memory leak is harmless because it does not accumulate, and the implicit memory leak is very harmful because it is more difficult to detect than frequent and accidental memory leaks

Avoid memory overflow

Oom is the out of Memory, which is typically used incorrectly by the writer of the program, such as the release of the freed memory resource, which has been unable to be reused and the memory is exhausted. The fundamental solution is to optimize your code: Do some processing on memory references, use soft references, virtual references and weak references, and do processing directly in memory when loading pictures in memory, such as boundary compression, establish dynamic reclaim memory mechanism, optimize heap memory allocation of Dalvik virtual machines, customize heap memory size, etc.

1. The cursor of the database is not closed

2. Construction adapter does not use caching Converview

3. Unregisterreceiver is not invoked after calling Registerreceiver

4. Inputstream/outputstream not closed

5.Bitmap using recycle not called

6.Context leakage

7.static keywords, etc.

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.