Most read:
Popular archives:
JavaWorld's new look is here!
We've upgraded the site with a fresh look-and-feel, improved topical navigation, better search, new features, and expanded
community platform. Learn more about the changes to JavaWorld.
| Oracle Compatibility Developer's Guide |
| The Explosion in DBMS Choice |
Last month, Bill Venners wrote an excellent article on how garbage collectors works and a bit about how garbage collection is implemented in Sun's Java Developer's Kit (JDK). The important point is that garbage collection isn't new; it simply hasn't been so obviously present in a language targeted at such a wide programmer group before.
Also, the mythology surrounding the slowness of garbage-collected systems is just that, myth. I can show that the number of
instructions executed is the same whether I call malloc() and free() or I only call malloc() and some other code calls free(). Further, I can show that a conservative garbage collector will never free something I'm currently using. The only non-zero
difference in this equation is the "detection" stage in which the garbage collector finds out what is and what isn't an unreferenced
piece of memory. Here I have the advantage since I know immediately that I no longer need a piece of allocated memory and thus can free it right away.
"Aha," you may say, "Chuck is against garbage collectors!" That is not correct because I have also tracked down too many bugs where I knew I wasn't going to use another piece of memory again so I freed the allocated memory, only to modify the code at some later date without that knowledge and in the process introduce a bug by using memory I had already freed. Further, the "cost" or time penalty of running a garbage collection thread (which needs only access memory) is greatly reduced on a fast processor that is spending most of its time waiting for activity on the keyboard or disk drive. In the final analysis, I prefer garbage-collected systems over non-collected systems like C much the same that I prefer C over assembly language. I can spend less time thinking about the mechanics of programming and more time concentrating on the program itself.
All that being said, why write a column on not using garbage collectors?
Like most programmers who originally used C, I am very sensitive to memory issues. Thus, I find it difficult to trust a garbage collector to manage memory better than I can. (This is different than saying a garbage collector can manage memory more accurately than I can, which is a given.) Better in this context means perhaps more efficiently. As a karate student in Los Angeles I was struck by how universal one of the fundamental principles of the martial arts was: "You don't need to block a blow that can't hit you." Applying this principle in this context becomes "You don't need to collect garbage that you don't generate."
I needed a terminal -- not some surplus VT100 to hook up on my desk but a virtual terminal I could hook up to Java applications on a Web page. You see, it is one thing to talk about writing HelloWorld in Java and try to describe it on a Web page; I wanted to be able to run it on a Web page. But it isn't an applet, it's an application, the difference being that it communicates with the real world via a console interface like many computer programs do, not via a GUI/Web interface like applets do.