Cover Data Structures and Algorithms with Object-Oriented Design Patterns in Java
next up previous contents index

Projects

  1. Devise and conduct a set of experiments to measure garbage collection overhead. For example, write a program that creates a specified number of garbage objects as quickly as possible. Determine the number of objects needed to trigger garbage collection. Measure the running time of your program when no garbage collection is performed and compare it to the running time observed when garbage collection is invoked.
  2. Java does not provide the means for accessing memory directly. Consequently, it is not possible to implement the Java heap in Java (without using native methods). Nevertheless, we can simulate a heap using a Java array of ints. Write a Java class that manages an array of ints. Your class should implement the following interface:
    public interface Heap
    {
        int acquire (int size);
        int release (int offset);
        int fetch (int offset);
        void store (int offset, int value);
    }
    The acquire method allocates a region of size consecutive ints in the array and returns the offset of the first byte in the region. The release method release a region of ints at the specified offset which was obtained previously using acquire. The fetch method is used to read a value from the array at the given offset and the store method writes a value into the array at the given offset.
  3.   Using an array of ints simulate the mark-and-sweep garbage collection as follows:
    1. Write a class that implements the Handle interface given below:
      public interface Handle
      {
          int getSize ();
          int fetchInt (int offset);
          Handle fetchReference (int offset);
          storeInt (int offset, int value);
          storeReference (int offset, Handle h);
      }
      A handle refers to an object that contains either ints or other handles. The size of an object is total the number of ints and handles it contains. The various store and fetch methods are used to insert and remove items from the object to which this handle refers.
    2. Write a class that implements the Heap interface given below
      public interface Heap
      {
          Handle acquire (int size);
          void release (Handle h);
          void collectGarbage ();
      }
      The acquire method allocates a handle and space in the heap for an object of the given size. The release method releases the given handle but does not reclaim the associated heap space. The collectGarbage method performs the actual garbage collection operation.
  4. Using the approach described in Project gif, implement a simulation of mark-and-compact garbage collection.
  5. Using the approach described in Project gif implement a simulation of reference-counting garbage collection.


next up previous contents index

Bruno Copyright © 1998 by Bruno R. Preiss, P.Eng. All rights reserved.