[Mono-dev] Fixing some memory leaks on Windows (GC related)

Aras Pranckevicius aras at unity3d.com
Tue May 26 06:04:58 EDT 2009

We are investigating some memory leaks in Mono that happen on Windows. In
our (Unity) case, we live in a web browser and when closing our plugin the
process does not terminate. Turns out there are some GC related memory leaks
that persist after shutting down Mono. We are using quite an old Mono
version, but from a quick glance at 2.4 and svn, looks like they could be
there as well.

We found that there are some VirtualAlloc() calls that are never freed. They
mostly manifest themselves when allocating large managed arrays (it looks
like for large memory blocks a different allocation code path is used).

What we changed (everything on Windows):

1) add a call to GC_win32_free_heap() at the end of mono_jit_cleanup

2) change GC_win32_free_heap() to do VirtualFree as well (added whole "else"

void GC_win32_free_heap ()
    if (GC_no_win32_dlls) {
        while (GC_n_heap_bases > 0) {
            GlobalFree (GC_heap_bases[--GC_n_heap_bases]);
            GC_heap_bases[GC_n_heap_bases] = 0;
    } else {
        while (GC_n_heap_bases > 0) {
            VirtualFree(GC_heap_bases[--GC_n_heap_bases], 0, MEM_RELEASE);
            GC_heap_bases[GC_n_heap_bases] = 0;

The above seems to fix major leaks. We are not totally sure if it's the
right thing to do, but it does look like some VirtualFree() calls were

Aras Pranckevičius
work: http://unity3d.com
home: http://aras-p.info
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20090526/d9a88707/attachment.html 

More information about the Mono-devel-list mailing list