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:
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.
- Write a function trace_gc that
returns such a record.
- 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.
- 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.
- Test this program by creating a trace file at the interactive loop
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.
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.
- 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
- 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
- Do the same work for the non tail recursive function.
- Compare trace results.