Bug 51535 - Disposing Pixbuf leaks memory
Summary: Disposing Pixbuf leaks memory
Status: RESOLVED FIXED
Alias: None
Product: Gtk#
Classification: Mono
Component: gtk-sharp ()
Version: 2.x
Hardware: PC Linux
: --- normal
Target Milestone: ---
Assignee: Marius Ungureanu
URL:
Depends on:
Blocks:
 
Reported: 2017-01-14 12:35 UTC by dk
Modified: 2017-05-23 18:45 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description dk 2017-01-14 12:35:34 UTC
Disposing a Pixbuf does not free the unmanaged memory used by it.

Observed on Arch Linux using:

  mono runtime: 4.6.1.3
  gtk-sharp:    2.12.42 (https://www.archlinux.org/packages/extra/x86_64/gtk-sharp-2/)

If I downgrade to gtk-sharp version 2.12.29 the issue is gone (this is the previous version provided by Arch Linux before it was upgraded to 2.12.42).

Sample to reproduce (memory usage is constantly increasing instead of staying at the same level as expected):


static int Main (string [] args)
{
    System.Diagnostics.Process currentProcess = System.Diagnostics.Process.GetCurrentProcess();

    for (int i = 0; i < 30; i++) {
        long totalBytesOfMemoryUsed = currentProcess.WorkingSet64;
        System.Console.WriteLine ("Memory: " + totalBytesOfMemoryUsed / 1024 / 1024 + " MB");
    
        using (var stream = new GLib.GioStream (GLib.FileFactory.NewForUri ("file:///path/to/image.jpg").Read (null))) {
            Gdk.Pixbuf p;
            using (var loader = new Gdk.PixbufLoader (stream)) {
                p = loader.Pixbuf;
                loader.Close ();
            }
            p.Dispose ();
            p = null;
        }
    }
    return 0;
}
Comment 1 Marius Ungureanu 2017-05-23 18:44:49 UTC
Since there is no glib main loop, dispose needs to be called. I have a patch to fix this, we were improperly refing the pixbuf (managed wrapper takes a ref, we don't need to ref more).

Here's the pull request for it - https://github.com/mono/gtk-sharp/pull/188
Comment 2 Marius Ungureanu 2017-05-23 18:45:02 UTC
Thank you for the bug report, helped me diagnose this easily.