In-depth illustration of Virtual Machine (I)-a question

Source: Internet
Author: User

1. What is memory leakage?

Memory leakage occurs when some objects are no longer used by applications and are not recognized as being no longer used (), so these objects cannot be recycled by the garbage collector, the occupied memory space cannot be released.

During the program running, the memory space will be constantly allocated. memory space that is no longer used should be recycled in a timely manner ,.

2. What causes memory leakage?

There are still some doubts in this article. GC is responsible for recycling objects that are no longer in use, so some objects are no longer in use. Why didn't GC recycle them?

In this case, we need to talk about the GC collection mechanism: Java uses the Reference method to access objects. If ObjectA no longer has references, GC will determine that ObjectA can be recycled, it will be recycled at a specific time. for example

                     Object objectA =          Object objectB =                   objectB = objectA;   }

When an Object loses all references, GC can recycle it, such as the Object in the graph heap. on the other hand, GC cannot recycle objects that are no longer in use if they are still referenced. just like a hotel, a hotel requires a room card. As long as I do not have a check-out card, the hotel attendant will not be able to recycle the room and let other tenants stay.

I saw a very good description in an article:

In the following situation, object A references object B. a's lifecycle (t1-t4) is much longer than B's (t2-t3. when object B is no longer used in the application logic, object A still holds the reference of object B. in this case, the garbage collector cannot release B from the memory. this situation may cause memory problems. If A still holds references from other objects, these referenced (useless) Objects will not be recycled and occupy the memory space.
B may even hold a lot of references to other objects. Because these objects are referenced by B, they are not recycled by the garbage collector. All these useless objects consume a lot of valuable memory space.

3. Example of Memory leakage

We can see a good example in crazy Java. Here we will describe that when ArrayList removes the last element, we only need to Reduce the length by one. for example, there are four elements in the past. To delete the last element, the length is changed to three, and the function has completed the purpose of deleting the last element. however, note that the ArrayList instance still has reference to the last element, but the last element is not used for the ArrayList, and GC does not recycle the fourth element. this causes memory leakage.

4. Online content analysis

At the time of writing this article, I also read many online articles and see an example:

         Vector v =  Vector(10          ( i = 1; i < 100; i++             Object o =               o =          }

In this example, we cyclically apply for an Object and put the applied Object into a Vector. If we only release the reference, the Vector still references the Object, therefore, this object cannot be recycled for GC. Therefore, if an object is added to a Vector, it must be deleted from the Vector. The simplest way is to set the Vector object to null.

The possible meaning in this article is that o = null indicates that o is useless, and the lifecycle should be over, while v still references it. GC cannot be recycled, so the memory is leaked.

I have a question here, because the lifecycle of v is not over yet. At this time, I want to retrieve 1st elements and use them correctly. Why is Object useless? GC does not recycle objects here, which is the correct decision. If the lifecycle of v is over, o will recycle the objects. Therefore,For general strong references, you do not need to deal with it intentionally. GC has been able to do a good job. It should be noted that the hidden object references in the Java class definition.

Here, we hope you will not be moved to the cloud.

5. Prevent Memory leakage

Memory leakage. To put it bluntly,A reference to an object with a long life cycle holds a short life cycle, and memory leakage is likely to occur. Although a short life cycle object is no longer needed, however, the object cannot be recycled because it is referenced by a long life cycle object.

Pay attention to the following aspects:

1) static variables

Static variables are not instance variables but class variables. The life cycle is the same as that of the application. Objects referenced by static variables cannot be released.

2) Collection class

The Collection class only has the method of adding elements, but does not have the corresponding deletion mechanism, resulting in memory usage. this is actually not clear. If this collection class is only a local variable, it will not cause memory leakage. If it is not referenced after the method stack exits, it will be recycled normally by jvm. if this collection class is a global variable (such as a static attribute in the class and a Global map), there is no corresponding deletion mechanism, which may cause the memory occupied by the set to only increase or decrease, therefore, it is necessary to provide such a deletion mechanism or regularly clear policies. here we will talk about cache content.

3) Singleton Mode

Improper use of the singleton mode is a common problem that causes memory leakage. A singleton object will exist throughout the JVM lifecycle after being initialized (in the form of static variables ), if a singleton object holds an external object reference, this external object cannot be Normally recycled by jvm, leading to memory leakage.

 

Related Articles:

1. What is a Memory Leak in Java?

2. Java Memory leakage

3. Will there be Memory leakage in java? Please briefly describe it

4. Crazy Java

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.