Taking Out the Garbage

By Jeff Cogswell  |  Posted 2008-09-17 Print this article Print


Collecting the Garbage

One of the huge problems with languages like JavaScript is garbage collection. Garbage collection just refers to the problem of using memory and being done with the memory but the computer not being able to reuse the memory because it doesn't know if you still need it. Got that?

If not, here's how it works: Suppose I create a Rectangle object. I use that in a calculation. Then I'm done with the object. In a language like C++, you write a line of code that tells the run-time library that you're done with the object. The run-time library will then delete the object and allow the part of memory where the object was stored to be used again for other objects.

But in languages like JavaScript, you don't explicitly tell the run-time you're done with the object. The run-time does, however, have ways of figuring out that you're done with the object. Typically, if a function creates an object, when the function is finished the object can be cleaned up too. But not always: The function might pass the object on to three more functions to use, for example, and only after the run-time system figures out that all four functions-the original and the three others-are finished can it figure out that the object isn't needed anymore and free the memory.

But the problem is the object might get saved away as a member within yet another object. Then the run-time can't delete the first object until this other object is no longer needed! Can you say, big mess? Yes, there is more than one reason it's called "garbage collection," and I'm sure some people would like to include other choice words instead of garbage, as well as directives about other people cleaning up their own, uh, messes.

Garbage collection is always a problem with languages, because often the actual garbage collector (the code that does the cleaning) only runs occasionally, and when it does, the program slows waaaaaaay down.

Google describes in its developer documentation a sophisticated garbage collector that Chrome uses. This collector stops everything and does its job very quickly while only collecting what's necessary, and then allowing everything else to resume. Personally, that sounds suspicious to me; however, I can understand how that could be better than having the garbage collector slowly churn away for minutes on end while you're continuing to do your work, albeit slowly, due to the garbage collector slowing everything down-and, with the work you're doing slowing the garbage collector down, causing it to run even longer. So perhaps this new method is better.

Jeff Cogswell is the author of Designing Highly Useable Software (http://www.amazon.com/dp/0782143016) among other books and is the owner/operator of CogsMedia Training and Consulting.Currently Jeff is a senior editor with Ziff Davis Enterprise. Prior to joining Ziff, he spent about 15 years as a software engineer, working on Windows and Unix systems, mastering C++, PHP, and ASP.NET development. He has written over a dozen books.

Submit a Comment

Loading Comments...
Manage your Newsletters: Login   Register My Newsletters

Rocket Fuel