# Pixbuf - memory leak?

**URL:** <https://www.ruby-forum.com/t/pixbuf-memory-leak/137880>\
**Category:** Ruby-Gnome 2\
**Created:** [May 17, 2008, 3:31pm UTC](https://www.ruby-forum.com/t/pixbuf-memory-leak/137880 "2008-05-17T15:31:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![prhlava](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/71e660/32.png) [@prhlava](https://www.ruby-forum.com/u/prhlava)\
**Post date:** [May 17, 2008, 3:31pm UTC](https://www.ruby-forum.com/t/pixbuf-memory-leak/137880/1 "2008-05-17T15:31:23Z")

</div>

Hello,

I am using gtk2 for image processing and the memory usage consistently  
grows (memory never gets reclaimed).

The code is along the lines:

# pixell coordinates to memory address of the first byte)

def p2i(px, py, channels, rowstride)  
px \* channels + py \* rowstride  
end

def process\_image(pixbuf, output\_dir)  
pixbuf.width.times do |x|  
pixbuf.height.times do |y|  
sample = pixbuf.pixels[p2i(x, y, pixbuf.n\_channels,  
pixbuf.rowstride), pixbuf.n\_channels]  
# do something with the sample here  
end  
end  
end

def process\_images(input\_dir, output\_dir)  
image\_paths = Dir.glob(input\_dir + “/\*.png”).sort

```
for ip in image_paths
    pixbuf = Gdk::Pixbuf.new(ip)

```

process\_image(pixbuf, output\_dir)  
end

end

Am i supposed to call garbage collection by hand or the gtk2 is not  
supposed to be used like this?

Vlad

---

<div class="post-metadata">

**Author:** ![prhlava](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/71e660/32.png) [@prhlava](https://www.ruby-forum.com/u/prhlava)\
**Post date:** [May 17, 2008, 4:43pm UTC](https://www.ruby-forum.com/t/pixbuf-memory-leak/137880/2 "2008-05-17T16:43:09Z")

</div>

Am Samstag, den 17.05.2008, 15:31 +0200 schrieb Vladimir K.:

> Hello,
> 
> I am using gtk2 for image processing and the memory usage consistently  
> grows (memory never gets reclaimed).

[snip]

> Am i supposed to call garbage collection by hand

yes, for now you have to start the GC on your own if you big amounts of  
data very fast.

Cheers, detlef

---

<div class="post-metadata">

**Author:** ![prhlava](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/71e660/32.png) [@prhlava](https://www.ruby-forum.com/u/prhlava)\
**Post date:** [May 17, 2008, 6:33pm UTC](https://www.ruby-forum.com/t/pixbuf-memory-leak/137880/3 "2008-05-17T18:33:56Z")

</div>

> > Hello,
> > 
> > I am using gtk2 for image processing and the memory usage consistently  
> > grows (memory never gets reclaimed).
> 
> [snip]
> 
> > Am i supposed to call garbage collection by hand
> 
> yes, for now you have to start the GC on your own if you big amounts of  
> data very fast.

Thanks for the pointer.

Well, to process one image takes minutes…

I have put the GC.start at the relevant place (in the cycle after  
process\_imate() call) and will see what happens…

Vlad

---

<div class="post-metadata">

**Author:** ![prhlava](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/71e660/32.png) [@prhlava](https://www.ruby-forum.com/u/prhlava)\
**Post date:** [May 17, 2008, 7:25pm UTC](https://www.ruby-forum.com/t/pixbuf-memory-leak/137880/4 "2008-05-17T19:25:54Z")

</div>

Am Samstag, den 17.05.2008, 19:14 +0200 schrieb Detlef R.:

Another speedup you’ll get, if you precalculate some variables like  
this:

pixels = pixbuf.pixels  
w = pixbuf.width  
h = pixbuf.height

h.times do |y|  
rs = y \* pixbuf.rowstride  
ch = pixbuf.n\_channels  
w.times do |x|  
sample = pixels[x \* ch + rs]  
end  
end

Cheers, detlef

---

<div class="post-metadata">

**Author:** ![prhlava](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/71e660/32.png) [@prhlava](https://www.ruby-forum.com/u/prhlava)\
**Post date:** [May 18, 2008, 6:36pm UTC](https://www.ruby-forum.com/t/pixbuf-memory-leak/137880/5 "2008-05-18T18:36:07Z")

</div>

Detlef R. wrote:

> you create for every single pixle an array with all pixels from the  
> pixbuf. That must be horrible slow. Change your method as followed:

(blushing for being stupid publicly 😉

Thanks to all who replied,

I have re-done the method (create pixels array once per picture) - it  
works faster and also it looks that the memory usage is OK.

Vlad

---

<div class="post-metadata">

**Author:** ![prhlava](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/71e660/32.png) [@prhlava](https://www.ruby-forum.com/u/prhlava)\
**Post date:** [May 17, 2008, 7:15pm UTC](https://www.ruby-forum.com/t/pixbuf-memory-leak/137880/6 "2008-05-17T19:15:21Z")

</div>

Am Samstag, den 17.05.2008, 18:33 +0200 schrieb Vladimir K.:

> > data very fast.
> 
> Thanks for the pointer.
> 
> Well, to process one image takes minutes…

Sorry, i’ve overlooked something in your example. Here

sample = pixbuf.pixels[p2i(x, y, pixbuf.n\_channels,  
pixbuf.rowstride), pixbuf.n\_channels]

you create for every single pixle an array with all pixels from the  
pixbuf. That must be horrible slow. Change your method as followed:

ï»¿def process\_image(pixbuf, output\_dir)  
pixels = ï»¿pixbuf.pixels  
pixbuf.width.times do |x|  
pixbuf.height.times do |y|  
sample = pixels[p2i(x, y, pixbuf.n\_channels,  
pixbuf.rowstride), pixbuf.n\_channels]  
# do something with the sample here  
end  
end  
end

Cheers, detlef
