Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Not using garbage collection

Minimize heap thrashing in your Java programs

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
No doubt about it, garbage collectors are weird. No, I don't mean the helpful men and women who go from door to door collecting refuse once a week. I mean programming systems where the programmer only allocates memory, and the system decides when to free it.

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."

The compelling issue

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.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources
  • WebTerm - An applet that emulates a VT100 and connects to hosts via a telnet client.
    http://www.nacse.org/web/webterm/
  • Java telnet applet - An applet that emulates a VT320 terminal. This applet is pretty cool since it separates the terminal capability and the character display capability into two pieces.
    http://www.first.gmd.de/persons/leo/java/Telnet/
  • Another telnet applet - This is similar to the other two. Source is available.
    http://w3.gwis.com/~thorn/telnet/
  • Sources - This page contains pointers to sources from this column (and others) as well as an example of the virtual terminal I built using these classes.
    http://www.mcmanis.com/~cmcmanis/java/javaworld/