Put memory 'garbage' collection to good use

Daily Newsletters

Sign up to ZDNet UK's daily newsletter.

ANALYSIS
Automatic memory management allows the developer to focus on application logic (e.g., payroll, solving math problems) instead of innate details, such as memory allocation. However, popular computing languages like C and C++ didn't have a standardised method for supporting automatic memory management until very recently. Popularity and standardisation occurred with the emergence of governed virtual machines that execute intermediate languages, such as the virtual machines used to run Java or the .Net languages. A deceptively difficult, yet interesting problem to solve is in the area of automatic memory management. Half of the problem, the act of allocating memory to a program, is relatively trivial -- the hard part is discovering when a program is truly finished with a piece of memory. Memory that is no longer needed, garbage memory, is collected by a garbage (memory) collector. The goal is to free unused memory as soon as it becomes garbage, so it can be reused later in the program if needed. Many different types of algorithms have surfaced to handle memory management. Yet, there still is no best solution for all cases. This article discusses some of most popular garbage collector (GC) algorithms used today in the Java and .Net virtual machines. Garbage collecting algorithms work either on the fly while objects are being referenced and dereferenced, or in snapshot mode as if periodically the memory picture of the application is frozen while the collection runs and finds the garbage. Reference counting
Probably the most intuitive automatic memory management algorithm is reference counting. If you keep track of which objects the program is referencing, those objects must still be needed. Implementing the algorithm requires that each object has a data field that is updated as to how many other program objects reference (i.e., point to) it. Any references of the object pointing to itself are ignored. If that count ever goes to zero, the object in question is considered garbage. After all, if no one references an object, it is, in effect, orphaned in memory. In terms of memory and performance, this is the most efficient way to collect garbage memory. Unfortunately, computer scientists face an impossible problem with this algorithm. If two objects or a large chain of objects point to each other, and that set of objects is orphaned from the system, there is no obvious way to see the cycle. Since all objects involved have a reference count of at least 1, they preserve their non garbage status. Because of this problem, reference counting isn't often used in modern VMs with any fervor. In fact, Java only uses reference counting with distributed objects (i.e., remote method invocation), which is borrowed from Modula-3's Network Objects. Distributed memory management imposes new constraints on garbage collections. Things like slow object access and references that can disappear at the drop of a network must be dealt with. Even though reference counting can leave cyclical garbage object sets alive, for performance and practicality reasons, it is more suitable for garbage collecting than other algorithms. Mark and sweep
Mark and sweep garbage collecting seems to always be the first try when developers of new systems implement garbage collection. It is theoretically easier to implement than other systems, but keep in mind that easy is relative. This algorithm was used in many Java VMs in early versions and is still used as a subalgorithm in advanced garbage collectors today. Mark and sweep starts by traversing all pointers from some canonical system object. That object is key to the VM, as in: If that object were to not exist, the VM is going down (i.e., the program has finished execution). Each object encountered via the pointer traversal is marked as being visited. Recursively, all pointers from all encountered objects are then traversed. In effect, this operation traverses the entire pointer reference tree rooted at the system canonical object, marking all objects marked along the way. Once this stage is complete, the algorithm looks at its complete list of known objects in existence. If any objects are found unmarked, they are considered to be disconnected from the system (garbage). This algorithm is clean and simple but suffers from some nagging issues. First, it requires all program execution to halt while it performs a job. Changes to the reference tree, while it is in midtraversal, compromise the algorithm. This algorithm was responsible for the pauses in execution that Java was famous for in its early days. Also, repeated sweeps cause memory fragmentation, which makes the job harder on the allocator. Eventually, a memory defrag must be run to clear up the fragmentation, causing even more pauses in execution.

Post your comment

In order to post a comment you need to be registered and logged in.

You can also log in with Facebook. Log in or create your ZDNet UK account below

  • Login

Will not be displayed with your comment

By signing up for this service, you indicate that you agree to our Terms and Conditions and have read and understood our Privacy Policy. Questions about membership? Find the answers in the Community FAQ

Get ZDNet UK's daily newsletter

Enter your email address to sign up

ZDNet UK Live

JCB33

How dare film makers, artists or anybody that invests in creativity stop us pirating their works for free. I want to be able to walk into my local...

35 minutes ago by JCB33 on ACTA stumbles in Germany
Moley

@GrueMaster. I prefer horses for courses rather than one size fits all. I, and I suspect most other computer users, do not really wish to have...

3 hours ago by Moley on A tale of two distros: Ubuntu and Linux Mint
greycynic

The product that scares me every time I have to use it is the Office 2007 version of Excel. The first bug that I found was applying the median...

3 hours ago by greycynic on Ten flawed products that derail productivity
GrueMaster

Nice review and very informative. One thing I'd like to add (in reply to whs001's 1st question), the main reason to have the same interface from...

4 hours ago by GrueMaster on A tale of two distros: Ubuntu and Linux Mint
Frederick Wrigley

I'be been using Mint 12 since the RC came out, and I am far more happy with the Cinnamon, the Mate, and, yes (with extensions), theGnome 3...

5 hours ago by Frederick Wrigley via Facebook on A tale of two distros: Ubuntu and Linux Mint
bdantas

Excellent article. One small correction, though--although a fresh installation of Linux Mint 12 will, indeed, provide the user with a version of...

6 hours ago by bdantas on A tale of two distros: Ubuntu and Linux Mint
Alan Ralph

In related news, the ISPs club together to get the members of the Home Affairs Select Committee (ya goofed on that part, ZDNet UK) copies of "The...

6 hours ago by Alan Ralph via Facebook on MPs urge ISPs to take down terrorist material
Alan Ralph

In related news, the ISPs club together to get the members of the Home Affairs Select Committee (ya goofed on that part, ZDNet UK) copies of "The...

6 hours ago by Alan Ralph via Facebook on MPs urge ISPs to take down terrorist material
Moley

For Gnome 2 die-hards, it is possible to add icons to the bottom panel (or top top panel, if you prefer) which provide the exact Gnome 2...

7 hours ago by Moley on A tale of two distros: Ubuntu and Linux Mint
ramwellian

Your comments would seem pretty naive and immature. Your 'solution' appears to be, "gee, let's all just give in to the hackers and give them...

7 hours ago by ramwellian on Cloud computing security: no more oxymoron?
BugStalker

"Interesting thought ... If you installed Win7 as a dual boot on a machine that previously only had Linux, and it wrecked your Linux installation,...

8 hours ago by BugStalker on Windows 7 Declares War on GRUB
whs001

This is an excellent summary of Ubuntu and Mint and the interface differences between them. Most such articles take a very partisan position for...

8 hours ago by whs001 on A tale of two distros: Ubuntu and Linux Mint
Moley

@ewallace. Not so clear. Anyone can obtain the text, for example from here http://www.ustr.gov/webfm_send/2379. I support ACTA so long as it and...

8 hours ago by Moley on ACTA: Facts, misconceptions and questions
45283

I think WinRT is fantastic. I just wish it was an option for people that didn't want to go through Microsoft's App Store with its attendant...

11 hours ago by 45283 on Why Windows 8 needs architectural hygiene for WOA
Burn-IT

Nine people? £30m? Who's back pocket is that lot going in? And IF they say it is for new buildings, what about all the ones the government has...

12 hours ago by Burn-IT on Police set to launch three £30m e-crime hubs
ewallace

Just to be clear, nobody knows what is in the text of ACTA, here is a photograph of the text of ACTA http://twitpic.com/8h9iju as submitted to the...

12 hours ago by ewallace on ACTA: Facts, misconceptions and questions
fgvrg56

Unfortunately main issue is that ASUS is refusing to accept that they make some mistake on this version of asus Transformer prime. 1 - GPS sensor...

13 hours ago by fgvrg56 on Asus Eee Pad Transformer Prime Wi-Fi & GPS problems?
Ben Woods

@Marcus A fair question. Just talked with Archos which said it was working on an announcement for next week....

14 hours ago by Ben Woods on Archos confirms G9 Ice Cream Sandwich update schedule
Marcus Karlsson

Any update on this, considering the claimed "first week of February"?

16 hours ago by Marcus Karlsson via Facebook on Archos confirms G9 Ice Cream Sandwich update schedule
apexwm

Bill Goodrich : Just as al_langevin pointed out, with Windows Server 2008 there is no Services for Macintosh anymore. It's gone, not available....

24 hours ago by apexwm on Windows Server 2008 drops the ball for Mac compatibility

Latest in Application Development