Break Java myth (1) garbage collection solves all memory issues

zhaozj2021-02-16  86

Garbage collection solves all memory issues For Java programmers, garbage collection features are very helpful, and it is also a very advantage of using Java languages. However, the actual situation should not be considering the memory problem because the garbage collection can be cleared. It is to be indicated here that if this problem is ignored, then the problem will result. First, the garbage collection algorithm on different JVM is different, so if you think your program is able to run on a different JVM, you can't rely on the specific behavior of garbage collection. Garbage collection is a very active research problem, better, faster and more accurate collector is always in realization. However, many modern garbage collectors have the same problem. One is that when they run, not always release all those that can be collected. Analysis shows that most of the objects of the Java program are short-lived, so they will reduce the frequency of objects with longer life, which is based on most objects that need to improve performance. Survival, and those survival agents often continue to be referenced, so there is no need to check if such objects can be reclaimed every time you check. To release the memory of a specific object, you may need to call garbage collection multiple times. You can use the SYSTEM.GC method recommended (note that it is recommended) the garbage collector runs. The result of requesting this method usually causes a garbage collector to make a complete collection. Usually this is more thorough than the VM call garbage collection, and it will be done as fast as possible. If the programmer explicitly calls System.gc, then inference is more time to do more work (please note that there is more things to do more, this means a lot of inspections, remember Changes in the frequency of the frequency of the test of the long life period? Rather than really cleared). In either case (explicitly invoking garbage collection and VM call garbage collection) do not assume that all objects that can be collected will be realized. Explicit calls System.GC have a greater opportunity to complete thorough collection, but not to ensure it will be completed. The troubles encountered another programmer is that they tend to maintain references to those who are no longer needed. This will prevent the garbage collector from release the object. This situation will happen when you manage the list. Consider the following Objstack class. This class uses the PUSH and POP methods to manage objects in the stack. Both methods utilize an index, the index refers to the next available position in the stack. The PUSH method stores references to the new object and adds the index value, and the POP method reduces the index value and returns the top elements of the stack. Example 1: ObjstackClass Objstack; Private Int Index; Public Void Push (Object O) {Stack [Index] = O; Index ;} public object pop () {index-; return Stack [index];} // ...} Now create a capacity of 10 object, then call 8 PUSH methods to add objects to it, then the index value is 8. What happened now after considering the POP method three times? At this time, the index value is 5, but please note that there is no other change in addition to this index value. Although the POP method reduces the index value, it is actually a reference to those objects. Calling a POP method often means that those objects should be collected (most of the situation, even if it is not right, it is also after using the object later). However, since the stack still retains a reference to the object, it cannot be collected. These objects can only be collected only after calling PUSH.

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

New Post(0)