Garbage Collection Criticism

zhaozj2021-02-08  361

Garbage Collection Criticism

Published this article in Java, it seems a bit pointing to Java. In fact, the GC is a GC mechanism in all the characteristics of all a new generation of languages, Python, Eiffel, C #, Roby, etc. use the GC mechanism. But since the GC in Java is most famous, it should naturally be resistant to the sky.

This short message is from Comp.lang.java.Programmer with a big debate that occurs on Comp.lang.c , supports C and Java's two different forces to launch the first conflict in the new century, with the stickers more than 350, The two factions have a famous angular blanket. The C camp is the main reason for Pete Becker, ACM member, Dinkumware Ltd. Technology deputy director. This king is proficient in C and Java, has developed two language libraries, but it is very enthusiastic about C , and it is quite good for Java. When you talk about Java, it is okay. Once some people dare to use Java to criticize C , I can't help but jump, with the perseverance and the big fearless spirit and the opponent, the tongue war, even if only one left People also have to serve in the end. This gods are really few! I am really strange that he is on the usenet, don't you worry? His boss p.j. Plauger is so wide? The Java array protagonist is a brother of the net name Razzi. In addition, SUN has a famous Peter Van Der Van Der Van Der Van Der Van Der Van Der Van Der Van Der Van Der, and the words are lingering, inch soil must strive, plus more people, and occupy the advantage. Although there are many C camps, most of them don't have pete, such as Greg Comeau, Comeau's boss, every time I come to a word, I can't help Pete is too busy. However, since the C camp, an unknown kid, the net name Courage (courage), and the criticism of the Java GC mechanism was launched. The C camp is in full attack, the Java camp is tired of defending, only in the court: "You have no evidence, no statistics", the situation is very passive.

Waste collection (GC) is not used by java fans to show off, proud? How did it become weak? I am puzzled, I will find it in this way.

First, Java Swing inventory is very clear in a large number of resource leaks, which is very clear, called BUGS, is being amended. However, it seems that the problems here are probably not only the negligence of the writers of the library, but the root may be the deep mechanism, and may not be able to solve it easily. It is not good to hurt the bones. However, this problem is not that, the C camp feels that if the other party's weakness attack is seized, even if it takes up the wind, there is no convincing. Who has no shortcomings? So, in turn, violently attacking the Java camp feeling the most proud of the Java's GC mechanism itself.

First, think about it, Memory Leak means something. In C , New Objects have no delete, which results in Memoryleak. But C has long had a way to overcome this issue - Smart Pointer. By using exquisite auto_ptr and various STL containers in the standard library, there are, for example, the four smart points in the Boost library (almost the standard library), C programmers learn the latest information for a week, You can shoot your chest: "I wrote the program without Memory Leak!". In contrast, Java seems to be better, because you don't have to consider what special mechanisms from the beginning, boldly, new, self-owned GC to clean up the resilience. Java's GC is actually a stand-alone thread in the JVM, using different algorithm policies to collect the memory occupied by the spam objects in HEAP. However, in general, the priority of the GC thread is relatively low, and only when the current program is idle, it will be scheduled to collect garbage. Of course, if the JVM feels tight, JVM actively calls GC to collect garbage to get more memory. Note that the timing of Java's GC work is: 1. The current program is not busy, there is free time. 2. Insufficient free memory. Now we consider a common situation, the program is running in nervous, there is no idle time to run GC, and the machine memory is very large, and JVM has not felt insufficient memory, what is the result? By the way, the GC is similar to the dummy, and the call is not called. So, memory is constantly being swallowed, and those that have already used unexpected garbage still sleep in the valuable memory. E.g:

Class badgc {

Public void Job1 () {string garbage = "i am a garbage, and just sleeping in your precious memory," "How do you think you can deal with me? daydreaming! Haha !!!"; ....

Public void Job2 () {...}

...

Public void job1000 () {...}

Public static void main (string [] args) {bgc = new badgc (); bgc.job1 (); bgc.job2 (); ... bgc.job1000 ();}}

Operation, although the Garbage object is no longer used after leaving Job1 (). But because the program is busy, the memory is still enough, so the GC will not schedul, Garbage will not be recycled until the program is running to BGC.JOB1000 (), I still laugh at you. No need!

Ok, I admit that this program is stupid. But don't think that this is just theoretical assumption, just contrary, most practical Java programs have similar effects. That's why Java programs are mad memory, and it seems that there is not enough memory to eat. You spend a big bank to raise memory from 128 to 256, then raise to 512, the result is that once the complex task is performed, the memory is still easy to fill, and more of these memory is just used to install garbage, the GC is still not I can't call the face. Wait until your memory is finally interspersed, GC is late, clean up the residue. And the way GC work is also very good, one method is to reclaim all garbage once there is a chance to recover memory. You can imagine, this takes a long time (a few hundred m garbage!) Another method, after getting the opportunity, recover some memory, let JVM feel that the memory is not so nervous. As a result, the memory always has a large number of garbage, and the procedure is always unlikely. Finally, GC can run once every other period of time, only to recover some of the garbage every time, this is the way most JVM now, the result is that memory is also wasteful, but also stops a few hundred milliseconds. Difficult! In turn, see the effect of C to achieve the effect of Smart Pointer. Once an object is no longer referenced, the system is unbucosed, and the memory is recycled immediately. This usually occurs during the cleaning (Cleanup) period after the completion of the key task, does not affect the real-time task of critical tasks, and all objects in memory are useful, no garbage is in memory. how about it? Traditional, simple C is it more winning?

According to statistics, the memory occupied by the current Java program is usually 4-20 times the corresponding C program. In addition to other reasons, what is said is a very important factor. Our resentment for Memory Leak, isn't it because it causes a lot of memory garbage? If there is a GC, the garbage is more than before, then what is the benefit of GC?

Of course, there are not many people in the C Smart Pointer, so the current C program is generally more serious Memory Leak issues. However, if my grandmother lost the Schumacher competition, can you blame the car?

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

New Post(0)