by
[Help |
API Docs |
Short Course|
Exercises]
In this exercise you will observe the behavior of the
distributed garbage collector and the local garbage collector
working together to manage memory.
There is no code to write, but you have to
change the value of the constant HOST_NAME to
match your computer name.
You will also want to look
at the code of the classes that make up this example,
especially MessageObjectImpl
to understand exactly how it works.
In this exercise there are two remote objects, Hello
and MessageObject.
Their implementations are designed
to print out information when they are created, unreferenced, finalized
and then deleted.
A remote object can implement the Unreferenced
interface and its one method, unreferenced.
This method is called by the DGC when it removes
the last remote reference to the object.
MessageObjectImpl and HelloImpl are designed to print a message when
this happens.
MessageObjectImpl and HelloImpl also implement the finalize method.
This is called when the local garbage collector
is about to destroy an object and reclaim its memory space.
In this implementation, MessageObjectImpl and HelloImpl prints a message to the console.
The course material for this exercise is covered in
Distributed Garbage Collector.
To run this exercise, you run RMIServer and two copies
of the client, RMIClient.
As you run this exercise, you may want to experiment with the setting of the
Java heap size (use the -mx command line argument) and you may wish to
explicitly set the DGC remote reference leaseValue.
To change this, use the following command line:
java -Djava.rmi.dgc.leaseValue=10000 RMIServer
where the unit of time for leaseValue is in Milliseconds.
Skeleton Code
Hello.java
HelloImpl.java
MessageObject.java
MessageObjectImpl.java
RMIServer.java
RMIClient.java
Tasks
1. Change the value of the constant HOST_NAME in RMIClient.java
and RMIServer.java to match the name of your computer.
2. Compile all Java source files.
3. From the Solution subdirectory, run the server,
RMIServer with the smallest heap possible.
4. Run two instances of the client.
Where help exists, the task numbers above are linked to the
step-by-step help page.
Solution Source
Hello.java
HelloImpl.java
MessageObject.java
MessageObjectImpl.java
RMIServer.java
RMIClient.java
Demonstration
When the server is run in its DOS console, the output will be:
Registry created on host computer ROSA on port 10007
Remote HelloService implementation object created
Bindings Finished, waiting for client requests.
Then when the client is run in its DOS console, the output will be:
HelloService lookup successful
Message from Server: Hello!
As the two clients run, each DOS console will display messages.
The server console will display messages as follows:
MessageObject: Class Number is #1882 Object Number is #1882
MessageObject: Unreferenced for object #: 1841
MessageObject: Finalize for object #: 893
Each of the client consoles will display an output similar to this:
MessageObject: Class Number is #1882 Object Number is #1882
Observe how the Server console indicates when the GDC and local garbage collector are working.
Copyright 1996-2000 jGuru.com. All Rights Reserved.
|