Summary of Java memory leaks--types of references: strong references, weak references, soft references

Source: Internet
Author: User
Tags object object java reference

Today interviewed a company's Java development intern, was asked a question: How to deal with the memory leaks in Java, to ensure that the Java Virtual Machine memory will not be blown out, then actually feel that the interviewer's problem is a bit broad, so there is no good understanding of his meaning, the answer is not very accurate, Later went back to check the information, probably understand what the interviewer is to ask what (embarrassing, only to react to), and then, also specially simple summary of the Java memory overflow related content, in case of review later.

What is the case of a Java memory leak?

Doesn't Java have a GC? Do you have memory leaks for hair? I have always thought that Java has a garbage collector in the case of memory leakage is not generally expected to happen? It turns out that this is not the case, first look at the following code:

A simple example of a memory leak//first, to understand that the GC reclaims the unreachable object, but in the static collection class, the reference can be reached, but it is possible that the object has not been used//first define a static variable public static arraylist< object> list = new arraylist<object> ();p ublic void Stackoverexam (Object object) {//When a non-static variable holds a reference by a static variable, A memory leak is prone, because object is a List.add (object) that has been referenced by the list, and object = null;//is set to null here and does not reach the effect of releasing object references, after all, the list still holds a reference}

From the above code, it is estimated that you can also see: Because the static point object is not recycled by the garbage collector, so, the indirect object can not be recycled, when the business objects are large and many, there is a risk of memory leaks, So we can summarize the following rules: When a global static variable holds a local variable (or a large range of variables that hold a small range of variables and a small range of variables consumes large amounts of memory), the program has the risk of memory leaks, in general, similar examples, the object in a singleton pattern, the call between modules, References to internal classes are held and so on

Second, how to solve?

After Baidu, actually found that the idea of the solution is probably related to the following knowledge: Java object references have the following categories: Strong reference, soft reference, weak reference. Concrete look

To solve the memory leak problem, the main use is to explicitly declare the type of reference, first of all to explain the relevant knowledge:

1, the type of reference: Strong, soft, weak. Ordinary Java reference, is also our most common Java Reference object, is a strong reference, it directly affects the garbage collection (a strong reference to the corresponding object, the GC can not be recycled, even if the memory is not enough); soft references, and strong references, the main difference is that it affects the GC of the collection of objects, but , when the virtual machine has insufficient memory, the soft reference points to the object (which, of course, cannot have a strong reference to the object at the same time) and is forced to be reclaimed; the weak reference is less hierarchical, it cannot affect GC's garbage collection of objects, it can only reference and Access objects. Specific three references can be illustrated by the following code example:

Examples of use of soft and weak references public void Referenceexam () {Object Object  = new Object ();//This ordinary Object reference is a strong reference// Soft references Create a soft reference through softreference softreference<object> softobjreference = new softreference<object> (Object);// Get the real object reference by the Get Method Softobjreference.get ();//Weak reference WeakReference to associate an object weakreference<object> weakobjreference = New Weakreference<object> (object), Weakobjreference.get ();//This method returns a true object reference, and returns null if no strong reference or weak reference points to it

  

2, for the prevention of memory leaks and garbage collection related to the type of reference strength, we can summarize the following features:

A, when we cache large objects such as cached images to improve the reading speed (for large memory object problems), you can set the related objects to soft reference objects, so that when the memory is not enough, the GC can be recycled.

B. We can use weak references (equivalent to access to data only) that are not affected by the GC's collection of objects, when we are not at all like the GC payback period originally set by other modules that affect the object because of the current reference to the object.

C, of course, Java also has a reference-type variable called a virtual reference, which is actually not used much in reality, mainly used to track the object GC process, so here is not detailed.

Finally, as to when to set the object as a weak reference (or soft reference), the individual feels that it is necessary to use the object according to the specific business judgment, if the global object is too large, it is possible to cause a leak in the program, or we do not want to be unclear when the object can be recycled (factory through the interface to provide the object to You can use soft or weak references for object access.

Of course, the memory leak is a big topic, here is just a small part of the discussion, you have more ideas also welcome the exchange!

Summary of Java memory leaks--types of references: strong references, weak references, soft references

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.