| 1 |
= ProfilingAllocation |
| 2 |
|
| 3 |
With MLton and `mlprof`, you can <<Profiling#,profile>> your program to |
| 4 |
find out how many bytes each function allocates. To do so, compile |
| 5 |
your program with `-profile alloc`. For example, suppose that |
| 6 |
`list-rev.sml` is the following. |
| 7 |
|
| 8 |
[source,sml] |
| 9 |
---- |
| 10 |
include::https://raw.github.com/MLton/mlton/master/doc/examples/profiling/list-rev.sml[indent=0] |
| 11 |
---- |
| 12 |
|
| 13 |
Compile and run `list-rev` as follows. |
| 14 |
---- |
| 15 |
% mlton -profile alloc list-rev.sml |
| 16 |
% ./list-rev |
| 17 |
% mlprof -show-line true list-rev mlmon.out |
| 18 |
6,030,136 bytes allocated (108,336 bytes by GC) |
| 19 |
function cur |
| 20 |
----------------------- ----- |
| 21 |
append list-rev.sml: 1 97.6% |
| 22 |
<gc> 1.8% |
| 23 |
<main> 0.4% |
| 24 |
rev list-rev.sml: 6 0.2% |
| 25 |
---- |
| 26 |
|
| 27 |
The data shows that most of the allocation is done by the `append` |
| 28 |
function defined on line 1 of `list-rev.sml`. The table also shows |
| 29 |
how special functions like `gc` and `main` are handled: they are |
| 30 |
printed with surrounding brackets. C functions are displayed |
| 31 |
similarly. In this example, the allocation done by the garbage |
| 32 |
collector is due to stack growth, which is usually the case. |
| 33 |
|
| 34 |
The run-time performance impact of allocation profiling is noticeable, |
| 35 |
because it inserts additional C calls for object allocation. |
| 36 |
|
| 37 |
Compile with `-profile alloc -profile-branch true` to find out how |
| 38 |
much allocation is done in each branch of a function; see |
| 39 |
<<ProfilingCounts#>> for more details on `-profile-branch`. |