Deep understanding of memory overflows and leaks

Source: Internet
Author: User

Last week, I saw the teacher give the students a question: what is a memory leak, for example. It is interesting to listen to the change of the teacher said that there is a recursive problem, and then some students directly removed the export of recursion, as a memory leak example.


This makes me realize that a lot of people don't really know about memory leaks and memory overflows, so they simply don't understand the underlying concepts of memory leaks and memory overflows. So I wrote this article specifically, I hope that through the easy-to-understand language to let people have a clear distinction between the two.


So what is a memory leak, memory leaks, memories leak, refers to the program after the application of memory, can not release the memory space has been requested, a memory leak damage may be ignored, but the memory leak accumulation of serious consequences, no matter how much memory, sooner or later will be occupied by the light, It is necessary to say that the memory leaks will eventually cause memory overflow.


The reason for the memory leak in Java is clear: a long-life-cycle object that holds a reference to a short lifecycle object is likely to have a memory leak, although a short-life-cycle object is no longer needed, but because a long-life-cycle object holds its reference and cannot be recycled, this is the scenario where memory leaks occur in Java.

For example: improper use of singleton mode is a common problem that causes memory leaks, when a singleton object is initialized to exist (in the same way as a static variable) for the entire life of the JVM, and if the Singleton object holds a reference to an external object, the external object will not be properly reclaimed by the JVM, resulting in a memory leak.

Class a{
Public A () {
B.getinstance (). SetA (this);
}
}
Class B with single-case mode
Class b{
Private a A;
private static B Instance=new B ();
Public B () {}
public static B getinstance () {
return instance;
}

public void SetA (a a) {
This.a=a;
}
}


and memory overflow, memory overflow, out of memories. The summary is that you asked to allocate more memory than the system can give you, the system can not meet the requirements, and then overflow. For example, a float is applied, but a double is stored to save the number, which can cause memory overflow.


That's a lot of examples, like

while (true) {

int i = 1;

}

The direct death cycle causes the final space of the application space not satisfied, will overflow, or recursion when no exit will cause this phenomenon. It is obvious that this will eventually cause the program to crash.


So the interviewer usually asks you this question: What is a memory leak, what is the harm, an example, and how to solve it. As an efficient developer, we can not only find the problem, but also solve the problem, whether in development or in life, so as to embody a person's value.


So how to solve the memory leak or memory overflow, this has a memory leak in the article written in the comparison, here is quoted here:


1. Release the reference of the useless object as soon as possible. A good idea is to use a temporary variable when the reference variable is automatically set to null after exiting the active domain, implying that the garbage collector collects the object to prevent a memory leak.

2, our program is inevitably a lot of string processing, avoid the use of string, should use a lot of stringbuffer, each string object has to occupy the memory of an area

3, minimize the use of static variables, because the static variables are global, the GC will not be recycled;

4, avoid the centralized creation of objects, especially large objects, the JVM will suddenly need a lot of memory, it will inevitably trigger GC optimization system memory environment;

5, try to use the object pool technology to improve the system performance; objects with long life cycles are prone to memory leaks when they have short life-cycle objects, such as when large collection objects have large data volumes of business objects, consider chunking and then resolve a piece of policy that frees a piece.

6. Do not create objects in frequently invoked methods, especially when creating objects in loops. You can use Hashtable,vector to create a set of object containers and then fetch those objects from the container without discarding them each time you new

7, generally occurs in the opening of large files or with the database once took too much data, resulting in the state of the memory Error, it is likely to calculate the maximum amount of data, and set the required minimum and maximum memory space value


Summary: We really develop, we must avoid the problem of memory leaks, it is like a cancer, wait until the proliferation of the time is not good, keep good logic and thinking to programming is essential, and the two concepts we must master, one or two years of work experience in the interview when the interviewer should prefer to ask this question.

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.