Data Structures and Algorithms with Object-Oriented Design Patterns in Python
next up previous 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. Python does not provide the means for accessing memory directly. Consequently, it is not possible to implement the Python heap in Python (without using native methods). Nevertheless, we can simulate a heap using a Python array of ints. Write a Python class that manages an array of ints. Your class should implement the following methods:
    class Heap(object):
    
        def acquire(self, size):
    	"(Heap, int) -> int"
    	# ...
    
        def release(self, offset):
    	"(Heap, int) -> None"
    	# ...
    
        def __getitem__(self, offset):
    	"(Heap, int) -> int"
    	# ...
    
        def __setitem__(self, offset, value):
    	"(Heap, int, int) -> None"
    	# ...
    The acquire method allocates a region of size consecutive ints in the array and returns the offset of the first int in the region. The release method release a region of ints at the specified offset which was obtained previously using acquire. The __getitem__ and __setitem__ methods access a value in the array at a given offset.
  3.   Using an array of ints simulate the mark-and-sweep garbage collection as follows:
    1. Write a Handle class that contains the methods given below:
      class Handle(Object):
      
          def getSize(self):
      	"(Handle) -> int"
      	# ...
      
          getReference(self, offset):
      	"(Handle, int) -> Handle"
      	# ...
      
          setReference(self, offset, handle):
      	"(Handle, int, Handle) -> None"
      	# ...
      
          def __getitem__(self, offset):
      	"(Handle, int) -> int"
      	# ...
      
          def __setitem__(self, offset, value):
      	"(Handle, int, int) -> int"
      	# ...
      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 Heap class that contains the methods given below:
      class Heap(object):
      
          def acquire(self, size):
      	"(Heap, int) -> Handle"
      	# ...
      
          def release (self, handle):
      	"(Heap, Handle) -> None"
      	# ...
      
          def collectGarbage(self):
      	"(Heap) -> None"
      	#
      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 index

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