gotk3 / gotk3

Go bindings for GTK3
ISC License
2.11k stars 230 forks source link

Pixbuf memory leak #163

Closed lenonarddo closed 6 years ago

lenonarddo commented 7 years ago

I like your project and I try to apply it to build an application. But it looks like the Pixbuf library does not work well, I checked and get a lot of memory leak.

ugjka commented 7 years ago

I have a simililar problem with this code

func (w *video) update() {
    var err error
    w.tmpbuff, err = w.pixbuff.ScaleSimple(w.width, w.height, gdk.INTERP_NEAREST)
    fatal(err)
    w.image.SetFromPixbuf(w.tmpbuff)
}

I tried w.tmpbuff.Unref() after every update which seemed to work but after some time the whole thing blew up

ugjka commented 7 years ago

It seems having runtime.GC() on a ticker like every 20 seconds solves the problem. So the issue is with garbage collection in my case

raichu commented 6 years ago

This isn't working work me, the memory usage always goes up.

I checked the current code and the problem appears to be missing finalizers. (see here for example, it's an old fork of mine with my changes which precedes gotk3/gotk3 and it didn't have this memory leak problem back then).

In fact, I do remember fixing those memory leaks in 3489c6933 and apparently those finalizers have since been removed, re-introducing the memory leaks. no, it's wrapped inside glib.Take so I'm not sure what's going on.

raichu commented 6 years ago

Update: after loading several hundreds of images (runtime.GC() is run twice after each load), the OS tells me that memory usage is around 1G. pprof shows only 2MB for Go, so the memory leak is definitely on the C side, due to pixbuf/glib bindings.

raichu commented 6 years ago

OK, found the problem. glib.Take() calls Ref on Objects, but it shouldn't be called at all in almost all pixbuf-related functions.