Memory overflow and memory leaks

Source: Internet
Author: User

Memory leak refers to a situation in which a program fails to release memory that is no longer in use due to negligence or error, and is a waste of memory because of a programming error that causes the JVM to lose control of the memory because of a program failure. A general memory leak refers to a leak in heap memory. Heap memory is a block of memory allocated by the program from the heap (the size of the memory block can be determined during the program run time). Applications generally use functions such as new to allocate from the heap to a piece of memory, normally after the program is used to release the memory that is no longer used, if the memory used is not released, then this block of memory can not be reused, that is, a memory leak.

The memory leaks are categorized in 4 ways:

A, a common memory leak. The code that has a memory leak is executed multiple times, causing a memory leak each time it is executed.

b, sporadic memory leaks. Code that occurs with a memory leak occurs only under certain circumstances or during operation. The occurrence and the incidental sex are opposite. For a given environment, the occasional may become a frequent occurrence. So test environments and test methods are critical to detecting memory leaks.

C, disposable memory leaks. The code that occurs with a memory leak is only executed once, or because of an algorithmic flaw, there is always a block and only one piece of memory leaks. For example, allocating memory in a constructor for a Singleton class does not release the memory in the destructor, and the Singleton class only has one instance, so a memory leak occurs only once.

D, 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 the requested memory. But for a server program that needs to run for days, weeks, or months, not releasing memory in time can also 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 using the program, the memory leak itself does not have any harm, as a general user, there is no sense of memory leaks. What is really damaging is the accumulation of memory leaks, as this will sooner or later drain all of the system's memory. From this point of view, 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 the usual and sporadic memory leaks.

Memory overflow (out of memory) refers to an overflow that occurs when the program is running in a way that requires more than the system can allocate.

The following code is combined to analyze memory leaks and memory overflows:

Import Java.util.vector;public class Test {public static void main (string[] args) {vector<object> Vector = new Vecto r<object>; for (int i = 1; i < 999999000; i++) {Object obj = new Object (); Vector.add (obj); obj = null;}}}
Analysis: The above code loops through the creation of object objects and puts the created object into a vector, if we just release the reference itself (that is, the No. 08 line of code), then the vector still refers to the object, so this object is not recyclable to the GC, resulting in a memory leak, When the JVM can run out of memory allocated for creating object objects, the memory allocated by the JVM is "stretched" and no memory is allocated, resulting in a memory overflow when the object is created. This shows that the memory leak will eventually result in out of memory!

Memory overflow and memory leaks

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.