Previous Contents Next

Exercises

Following the evolution of the heap

In order to follow the evolution of the heap, we suggest writing a function that keeps information on the heap in the form of a record with the following format:

# type tr_gc = {state : Gc.stat;
time : float; number : int};;
The time corresponds to the number of milliseconds since the program began and the number serves to distinguish between calls. We use the function Unix.time (see chapter 18, page ??) which gives the running time in milliseconds.
  1. Write a function trace_gc that returns such a record.

  2. Modify this function so that it can save a value of type tr_gc in a file in the form of a persistant value. This new function needs an output channel in order to write. We use the Marshal module, described on page ??, to save the record.

  3. Write a stand-alone program, taking as input the name of a file containing records of type of tr_gc, and displaying the number of major and minor garbage collections.

  4. Test this program by creating a trace file at the interactive loop level.

Memory Allocation and Programming Styles

This exercise compares the effect of programming styles on the growth of the heap. To do this, we reconsider the exercise on prime numbers from chapter 8 page ??. We are trying to compare two versions, one tail-recursive and the other not, of the sieve of Eratosthenes.

  1. Write a tail-recursive function erart (this name needs fixing) that calculates the prime numbers in a given interval. Then write a function that takes an integer and returns the list of smaller prime numbers.

  2. By using the preceding functions, write a program (change the name) that takes the name of a file and a list of numbers on the command line and calculates, for each number given, the list of prime numbers smaller than it. This function creates a garbage collection trace in the indicated file. Trace commands from previous exercice are gathered in file trgc.ml

  3. Compile these files and create a stand-alone executable; test it with the following call, and display the result.
                                                               %
    erart trace_rt 3000 4000 5000 6000
    


  4. Do the same work for the non tail recursive function.

  5. Compare trace results.

Previous Contents Next