[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[ruby-gtk2] Garbage collection and memory use

David Espada

1/18/2005 8:59:00 AM

Hi all.

Look at next script:

----------------------------------------
#!/usr/bin/ruby

require 'glib2'
require 'gtk2'

def bucle
1000.times do |i|
100.times do |j|
p i
p j
p '******'
v = Gtk::Window.new.show_all
v.destroy
end
GC.start
end
end

Gtk::init

ventana = Gtk::Window.new
ventana.set_title("Creating windows")
ventana.signal_connect('delete_event') {exit}


vbox = Gtk::VBox::new(true, 10)

boton = Gtk::Button.new('_start', true)
boton.signal_connect('clicked') do bucle end
vbox.pack_start(boton, true, true, 0)

ventana.add(vbox)

ventana.show_all

Gtk.main
--------------------------------------------

When you push button, it triggers a loop that creates and destroy a
Gtk2 window in each iteration. For each 1000 iters, I call GC.start in
order to run Garbage Collection.

In theory, there is no single variable that persist from one iter to
next, and garbage collection should reget memory for more use, without
collapsing physical memory and swap space.

But that is what happens actually, and I don't know the reason.

You can say that this lanscape is very rare and not reproducible in
practice, but I have an application that creates and destroys windows
with many widgets, and it loads system until swaping is constant,
slowing work of my users.

What is problem with Garbage Collection? Why it doesn't free memory
althought I invoque it explicitly? Any clue or solution that doesn't
force me to change all code in order to reuse windows instead of
create them when I need?

I am in the very edge, but it seems that Ruby Garbage Collection
doesn't work properly with Gtk2 widgets, because memory is not freed.

Thanks for your ideas and help.

I use last version of Ruby (1.8.2) with last version of Ruby-gtk2
(0.11.0) in Linux Debian Sid, with kernel 2.6.7.

Greets.

David
1 Answer

Masao Mutoh

1/18/2005 4:53:00 PM

0

Hi,

On Tue, 18 Jan 2005 18:01:10 +0900
David Espada <davinciSINSPAM@escomposlinux.org> wrote:

> Hi all.
>
> Look at next script:

> When you push button, it triggers a loop that creates and destroy a
> Gtk2 window in each iteration. For each 1000 iters, I call GC.start in
> order to run Garbage Collection.
>
> In theory, there is no single variable that persist from one iter to
> next, and garbage collection should reget memory for more use, without
> collapsing physical memory and swap space.
>
> But that is what happens actually, and I don't know the reason.

> What is problem with Garbage Collection? Why it doesn't free memory
> althought I invoque it explicitly? Any clue or solution that doesn't
> force me to change all code in order to reuse windows instead of
> create them when I need?


Try the code below:
(gcc -o test `pkg-config gtk+-2.0 --libs --cflags` test.c)
---
#include <gtk/gtk.h>

int main(int argc, char** argv){
GtkWidget* window;
gtk_init(&argc, &argv);

while(TRUE){
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_show_all(window);
gtk_object_destroy(GTK_OBJECT(window));
}
gtk_main();
return 0;
}
---

You can notice it does not concern Ruby GC.
I'm not sure but gtk_widget_show_all() causes this problem.

> I am in the very edge, but it seems that Ruby Garbage Collection
> doesn't work properly with Gtk2 widgets, because memory is not freed.

Any solutions?

P.S.
I don't think it's not good idea to post these discussions here.
If you want to discuss more, come ruby-gnome2 ML again.