[Mono-devel-list] GC with Pixbufs

Ben Maurer bmaurer at users.sourceforge.net
Mon Mar 15 15:52:47 EST 2004


On Mon, 2004-03-15 at 15:13, Miguel de Icaza wrote:
> For example, say that the Pixbuf takes 32 bytes of RAM, but the actual
> data pointed to takes 6 megabytes.  If you allocate 256 of these
> objects, as far as the GC is concerned, you have barely allocated 8k of
> GC memory, so it is not worth doing a collection for that little memory.

In v2, they have added a solution for this:
        public static Void AddMemoryPressure(
            Int64 pressure
        );
                
        `The garbage collector cannot take unmanaged resources into
        account when determining the urgency of collecting a particular
        object. Thus, if a small managed object holds much larger
        unmanaged resources, the garbage collector takes into account
        only the size of the managed object. AddMemoryPressure provides
        a way to increase the priority of collection.
        
        `For example, the author of a class that represents a bitmap
        might call AddMemoryPressure immediately before allocating
        unmanaged memory to hold the bitmap, and RemoveMemoryPressure
        immediately after releasing the unmanaged memory in the Dispose
        or Finalize method. The value specified for pressure might be
        the size of the bitmap.'
        (http://longhorn.msdn.microsoft.com/lhsdk/ref/ns/system/c/gc/m/addmemorypressure.aspx)
        
It would be interesting if Gtk# could use this hint once the 2.0 api is
finalized. It also might be worthwhile to look into adding some mono <->
gtk# magic (optional of course, so we can run on MS) in the meantime.

This could also help with other resources such as pooled database
connections, file handles, etc.

Note, however, the best solution, by far, is as miguel mentioned to use
the Dispose pattern. Using dispose will *always* release memory faster.

-- Ben





More information about the Mono-devel-list mailing list