O'Reilly logo

Java RMI by William Grosso

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Distributed Garbage Collection

Another one of the three fixed object identifiers is DGC_ID. This is the object identifier associated with the distributed garbage collector built into RMI. The distributed garbage collector is useful and important in distributed applications built using RMI. It’s also not very complicated. Before describing how the distributed garbage collector works, however, I will spend a few paragraphs explaining ordinary garbage collection.

Ordinary Garbage Collection

The motivation for garbage collection is simple: memory management is hard. Programmers are very good at creating new objects. But knowing when to release an object is much more difficult. Consider, for example, the following snippet of Java code:

public Money getBalance(String clientIDNumber) throws RemoteException,
    LockedAccountException {
		checkAccess(clientIDNumber);
		return _balance;
}

The problem is that, without some form of automatic memory management, programmers must somehow decide when _balance is freed. A consistent policy must be adopted across the entire codebase. Any failure, anywhere in the code, to abide by and enforce the policy, can lead to a memory leak. One such policy, for example, is:

If a method returns an object, the returned object is the responsibility of the caller. The object whose method was called should not retain any references to returned objects.

This policy is easy enough to implement; it changes the preceding method into:

public Money getBalance(String clientIDNumber) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required