Analysis of Java Memory Leakage Problem

xiaoxiao2021-03-06  39

Many people are talking about memory leaks. Of course, for C / C , this should be old-fashioned, but many Java people have more and more to discuss this problem, I write a summary here, I hope to have a certain reference to everyone. value. Solid leaks 1.c / c is the programmer management memory, and Java memory is automatically recovered by GC. Although I am not very familiar with C , this should have no common sense mistake. 2. What is memory leak? Memory leakage refers to memory in the system that cannot be recovered, sometimes causes insufficient memory or system crash. The case where memory is not released in C / C is memory leak. 3. The existence of memory leaks in Java We must first recognize this before it can be discussed. Although Java has memory leaks, it is basically not very concerned about it, especially those who don't pay attention to the code itself, don't pay attention to this. The memory leak in Java is of course referring to: an object that is useless but the garbage collector cannot be recycled. And even if there is a memory leak problem, it is not necessarily shown. 4. Java in parameters are transmitted. For basic types, everyone is basically no objection, but we can't have objections for reference types. Java memory leaks The JVM recycling algorithm is very complicated. I don't know how they implement it, but I only know that they want to implement: For objects that are not referenced, it is recycled. So you have to cause memory leaks to do: hold a reference to the useless object! Don't think this is easy to do, since it is useless, how can you hold it? Since you still hold it, how can it? Is it useless? I really can't think of more classic examples than that stack, so that I have to quote someone else's example, the following example is not what I think, is the book I saw, of course, if I didn't see it on the book. I may have thought about it for a while, but I said that I didn't believe in myself.

Public class stack {private object [] Elements = new object [= 0]; public void push (Object E) {ENSURECAPACITY (); ELECAPACITY (); ELECAPACITY (); ELECAPACITY = E;} public object pop () {= 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, then all bounce out, although the stack is empty There is no thing we want, but this is an object that cannot be recycled. This is in line with the two conditions of memory leak: useless, unable to recover. However, there is such a thing that does not necessarily lead to what kind of consequences. If this stack is less, it is astounding a few K memory. Anyway, our memory is G. There is any influence, and then This thing will soon be recovered, what is the relationship. Look at two examples below. Example 1

Public class bad {public static stack s = stack (); static {s.push (new object ()); S.POP (); // Here there is an object in memory leak S.Push (new object ()); // The object above can be recovered, is equal to itself}} because it is STATIC, there is always a program exit, but we can also see that it has its own feature, that is, if your Stack has up to 100 Object, then only 100 objects can not be recovered. This should be easily understood. STACK has 100 references, the worst case is that they are all useless, because we have new progress, the previous reference Natural disappearance! Example 2PUBLIC CLASS NOTTOOBAD {PUBLIC VOID DOSMETHING () {stack s = new stack (); s.push (new object ()); // j j (); // Here, the object cannot be recycled, memory leakage } // Exit method, S is automatically invalid, S can be recovered, the reference to the STACK is naturally gone, so // can also be self-healing here, and it can be said that this method does not have memory leak problems, but it is a little late // Give GC, because it is closed, it is not open, you can say that the above code 99.9999% // is not impact, of course, what you write does not have any bad influence, but / / / Absolutely can be said to be a garbage code! There is no contradiction, I will not have an empty for loop, I don't have anything too much. Do you do this?} The above two examples are just a small fight. Small trouble, but the memory leak in C / C is not Bad, but Worst. If they have no recycling, they will never be recycled. The frequent calls are not used in this method! Because Java has self-healing function (my name, I haven't applied for a patent), so Java's memory leakage problem is almost It can be ignored, but people know that people don't make it. I don't know anyone! Java has memory leaks, but don't exaggerate their words. If you are not very familiar with Java, you don't have to care about this, I said that you accidentally write memory leaks, just like you have a small probability, joking, it should be small. More! And even if you have a fortunate to write this code, winning! Basically, a pack of washing powder, will not let you make a fortune, there is no big impact on the system. The situation of worrying about people 1. No words can be said

Object obj = new object (); obj = null; // This completely, because withdraws the scope of action, the reference to the object is automatically disappeared // Don't appear in your program, there is no mistake, but it is not anger 2 Reflections

Void func (Object O) {o = new object (); return} When we know that the Java parameter is a pass value, you know that the above method is not wrong, it is to apply for an object and then lose GC. Because it is a pass value, here is a copy of a call, will it be unable to recover? Isn't it a copy, how to exit the method, how can this object stay? 3. Try to avoid

Class a {b = new b (this);} Class B {a a; b (a a) {this.a = a;}} This existence is cited to each other, which may cause the island phenomenon, but this will not cause memory disclosure However, I think this will reduce the efficiency of GC. From my intelligence, I think this situation is difficult to judge how to recover than the general situation! Of course, GC is smart than me, but it should also have a big brain.

转载请注明原文地址:https://www.9cbs.com/read-67882.html

New Post(0)