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