Java Memory leakage analysis

Source: Internet
Author: User
Java Memory leakage analysis
Author: csdn Source: csdn
Many people are talking about memory leakage. Of course, for C/C ++, this should be an old problem, but many Java people are getting to discuss it more and more, I will write a summary here, hoping to provide some reference value for you. Memory leakage refers to 1.c/ C ++, which is managed by programmers themselves, and Java memory is automatically reclaimed by GC. Although I am not very familiar with C ++, I should have not made a common sense error. 2. What is memory leakage? Memory leakage refers to memory that cannot be recycled in the system. Sometimes it may cause insufficient memory or system crash. If no memory is released in C/C ++, the memory leakage occurs. 3. We must acknowledge the memory leakage in Java before we can proceed with the discussion. Although Java memory leaks, you don't have to worry about it, especially those that are not very concerned about the Code itself. Memory leakage in Java is of course an object that is useless but cannot be recycled by the garbage collector. In addition, even if there is a memory leakage problem, it may not be displayed. 4. All parameters in Java are passed values. There is basically no objection to the basic type, but we cannot disagree with the reference type. Java Memory leakage The JVM Reclaim Algorithm is very complicated. I don't know how they implement it, but I only know what they want to implement: objects that are not referenced can be recycled. Therefore, if you want to cause memory leakage, you must: Hold a reference to useless objects! Don't think this is easy to do. Since it is useless, how can you still hold its reference? Since you still hold it, how can it be useless? I really can't think of a more classic example than that stack, so I want to reference other people's examples. The example below is not what I think, but what I see in the book. Of course, if I don't see it in the book, maybe I thought about it for a while, but at that time I said I did not believe it. Public class Stack {
Private object [] elements = new object [10];
Private int size = 0; Public void push (Object E ){
Ensurecapacity ();
Elements [size ++] = E;
} Public object POP (){
If (size = 0)
Throw new emptystackexception ();
Return elements [-- size];
} Private void ensurecapacity (){
If (elements. Length = size ){
Object [] oldelements = elements;
Elements = new object [2 * elements. Length + 1];
System. arraycopy (oldelements, 0, elements, 0, size );
}
}
} The above principle should be very simple. If the stack adds 10 elements and then all pops up, although the stack is empty, there is nothing we need, but this is an object that cannot be recycled. This meets the two conditions for Memory leakage: useless and irretrievable. However, the existence of such a thing may not necessarily lead to any consequence. If this stack is used less often, several K of memory will be wasted. In any case, our memory is on G, where will there be any impact? Besides, this stuff will be recycled soon. What's the relationship. The following are two examples. Example 1 public class bad {
Public static stack S = stack ();
Static {
S. Push (new object ());
S. Pop (); // There is an object with Memory leakage.
S. Push (new object (); // The above object can be recycled, so it is self-healing.
}
} Because it is static, it always exists until the program exits, but we can also see that it has a self-healing function, that is, if your stack has a maximum of 100 objects, in this case, at most 100 objects cannot be recycled. In fact, this should be easy to understand. The stack holds 100 references internally. The worst case is that they are useless, because once we make a new move, the previous reference will naturally disappear! Example 2 public class nottoobad {
Public void dosomething (){
Stack S = new stack ();
S. Push (new object ());
// Other code
S. Pop (); // The object cannot be recycled and the memory is leaked.
} // Exit method, S is automatically invalid, s can be recycled, and the reference inside the stack is naturally gone, so
// This method can also be self-healing, and it can be said that there is no memory leakage problem, but it is a little late
// It is only handed over to GC because it is closed and not open to the outside world. It can be said that the above Code 99.9999%
// The situation will not have any impact. Of course, writing such code will not have any bad impact,
// It is definitely a spam code! There is no conflict. I will not add an empty for loop in it.
// What is the major impact? Will you do this?
} In the above two examples, the memory leakage in C/C ++ is not bad, but worst. If one of them is not recycled, they will never be able to recycle it. If they call this method frequently, the memory will not be used up! Because Java also has a self-healing function (my own name has not yet applied for a patent), the Java Memory leakage problem can be almost ignored, but people who know it should not commit it. No one knows! Java Memory leakage, but do not exaggerate. If you are not familiar with Java, you don't have to worry about it. I said that you accidentally wrote an example of Memory leakage, just as you have a low probability of 10,000,001. You are joking, it should be much smaller! And even if you are lucky enough to write such code, you won the prize! Basically, it is a pack of washing powder that won't make you rich and has no major impact on the system. Worrying: 1. Nothing to say: Object OBJ = new object ();
OBJ = NULL;
// This is all the more, because the reference of the object disappears automatically because it is out of the scope.
// Do not display such a statement in your program. It is correct, but not elegant. 2. Think about the incorrect void func (Object O ){
O = new object ();
Return
} When we know that the Java parameter is a value transfer, we know that the above method is correct, that is, we applied for an object and then threw it to the GC. Because it is a value transfer, Here O is a copy of the call, will it be unable to be recycled? Isn't it a copy? There is no exit method. How can this object be kept. 3. Avoid type Class {
B = new B (this );
}
Class B {
A;
B (A) {This. A = ;}
} There is mutual reference, which may lead to islands, but this will not cause memory leakage. However, I think this will reduce the GC efficiency. From my perspective, I think this situation is more difficult to judge how to recycle than the general situation! Of course, GC is smarter than me, but you should have a bit of brains.

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.