[MLton] Is it safe to use an alternative malloc with Mlton programs?

Matthew Fluet fluet at tti-c.org
Tue Jan 20 19:57:30 PST 2009


On Tue, 20 Jan 2009, Vesa Karvonen wrote:
> On Tue, Jan 20, 2009 at 3:39 AM, Ville Tuulos <tuulos at gmail.com> wrote:
> [...]
>> Based on quick grepping of MLton's sources, it appears that malloc()
>> is not used in many places - I assume that internal memory handling is
>> done by mmap().
>
> AFAIK that is the case.  MLton's GC uses mmap to allocate memory for
> the ML heap.

Correct.  Profiling and hash-consing will use malloc to allocate memory 
outside the ML heap (and unmanaged by the MLton GC).

>> If this is the case, is it safe to link MLton / C code against TCMalloc?
>
> I don't see how it could be unsafe, but I don't know MLton's GC
> implementation very well.

I agree that, if TCMalloc malloc provides the same behavior as glibc 
malloc, then it should be safe.

> The only potential problem that comes into
> mind might be that if you incrementally allocate lots of memory from
> both the "C heap" (allocated with malloc (whether glibc malloc or
> TCMalloc)) and the ML heap, then, if/when a machine has a small
> address space relative to the total amount of memory used, you might
> fragment the address space so that MLton's GC fails to allocate (via
> mmap) a sufficiently large contiguous block of memory for the ML heap
> (when the heap needs to grow (or shrink)).

That is an issue, but as you note it is an issue with any C-side 
allocation.

-Matthew




More information about the MLton mailing list