ruby gtk-3 上的内存泄漏
Memory leak on ruby gtk-3
我在 ruby 2.3.1 和 gtk-3 上遇到内存泄漏。
在我的系统 (Ubuntu 16-04) 上,以下代码占用大约 80 MB。
picture.jpg 的大小是 289kb。
`需要'gtk3'
def ptest
i=0
j=0
loop {
i += 1
j += 1
exit if j==50
@image = Gtk::Image.new
newPixbuf = GdkPixbuf::Pixbuf.new(:file => "picture.jpg")
@image.pixbuf = newPixbuf
@image.clear
@image=nil
if i == 10
p "GC"
GC.start
i = 0
end
}
end
ptest`
根据https://sourceforge.net/p/ruby-gnome2/mailman/message/8659687/,这不应该发生。我该怎么做才能释放内存?
我不喜欢 Ruby 但知道一些 Gtk+。
在 C 中,您必须自己处理内存分配,您需要取消引用 pixbuf。
The GtkImage does not assume a reference to the pixbuf; you still need to unref it if you own references.
所以,很可能,如果 Ruby 没有实现 ARC(GObjects 上的自动引用计数),你必须做类似 newPixbuf.unref
的事情(不确定 Ruby 语法)在 @image.pixbuf = newPixbuf
.
之后
希望对您有所帮助。
一开始我不知道Ruby。
尽管您的 "picture.jpg" 文件为 289 KB,但它会比未压缩时使用更多的内存 space。
您正在创建 Gtk::Image 并将您的图像添加到其中。 GTK+ 会创建这个 OK,但是当你销毁它时,GTK+ 分配给它的内存不会立即释放。
来自 GTK+ C 文档
It's important to notice that gtk_widget_destroy() will only cause the
widget to be finalized if no additional references, acquired using
g_object_ref(), are held on it. In case additional references are in
place, the widget will be in an "inert" state after calling this
function; widget will still point to valid memory, allowing you to
release the references you hold, but you may not query the widget's
own state.
当控制返回给 GTK+ 时应该释放内存 "main loop"。您的示例中哪些尚未完成,哪些尚未初始化或启动。
显然我的 Ruby gdk3-Gem 中存在错误。 gem 更新解决了这个问题。
我在 ruby 2.3.1 和 gtk-3 上遇到内存泄漏。 在我的系统 (Ubuntu 16-04) 上,以下代码占用大约 80 MB。 picture.jpg 的大小是 289kb。
`需要'gtk3'
def ptest
i=0
j=0
loop {
i += 1
j += 1
exit if j==50
@image = Gtk::Image.new
newPixbuf = GdkPixbuf::Pixbuf.new(:file => "picture.jpg")
@image.pixbuf = newPixbuf
@image.clear
@image=nil
if i == 10
p "GC"
GC.start
i = 0
end
}
end
ptest`
根据https://sourceforge.net/p/ruby-gnome2/mailman/message/8659687/,这不应该发生。我该怎么做才能释放内存?
我不喜欢 Ruby 但知道一些 Gtk+。 在 C 中,您必须自己处理内存分配,您需要取消引用 pixbuf。
The GtkImage does not assume a reference to the pixbuf; you still need to unref it if you own references.
所以,很可能,如果 Ruby 没有实现 ARC(GObjects 上的自动引用计数),你必须做类似 newPixbuf.unref
的事情(不确定 Ruby 语法)在 @image.pixbuf = newPixbuf
.
希望对您有所帮助。
一开始我不知道Ruby。
尽管您的 "picture.jpg" 文件为 289 KB,但它会比未压缩时使用更多的内存 space。
您正在创建 Gtk::Image 并将您的图像添加到其中。 GTK+ 会创建这个 OK,但是当你销毁它时,GTK+ 分配给它的内存不会立即释放。
来自 GTK+ C 文档
It's important to notice that gtk_widget_destroy() will only cause the widget to be finalized if no additional references, acquired using g_object_ref(), are held on it. In case additional references are in place, the widget will be in an "inert" state after calling this function; widget will still point to valid memory, allowing you to release the references you hold, but you may not query the widget's own state.
当控制返回给 GTK+ 时应该释放内存 "main loop"。您的示例中哪些尚未完成,哪些尚未初始化或启动。
显然我的 Ruby gdk3-Gem 中存在错误。 gem 更新解决了这个问题。