【发布时间】:2016-05-09 15:31:15
【问题描述】:
RubyMotion 应该做自动内存管理:
RubyMotion 提供自动内存管理;你不需要 回收未使用的对象。
但是,在循环读取大文件时,我遇到了巨大的内存泄漏:每秒数百 MB/s,就像我的读取缓冲区从未释放一样。
如果我在每个循环的读取缓冲区上使用release,泄漏大多会消失。问题是,release 在循环结束时使应用程序崩溃。
def readBigBinaryFile(file)
# PURE RUBY WOULD BE
# source=File.open(file,'r')
source =NSFileHandle.fileHandleForReadingAtPath(file)
buffer_size=4096
offset=0
size=File.size(file)
while ( offset + buffer_size ) <= size
# PURE RUBY WOULD BE
# source.seek(offset)
# abuffer = source.read( buffer_size )
# abuffer=abuffer.to_s
source.seekToFileOffset(offset)
abuffer = source.readDataOfLength(buffer_size)
offset+=buffer_size
@dataPointer ||= Pointer.new(:uchar,4)
abuffer.getBytes(@dataPointer, length: 4)
# memory leaks very rapidly in GBs if we don't release the buffer…
# but this relase action will make the application crash once the doSomething lookp is finished
abuffer.release
end
source.closeFile
return false
end
循环是:
x=0
while x < 10000
my_scan_binary_instance=Scan_binary.new() result=my_scan_binary_instance.readBigBinaryFile(NSBundle.mainBundle.pathForResource("sample1MBfile", ofType:"img"))
puts result.to_s
x+=1
end
puts "if we have used 'abuffer.release', we are going to crash now"
我测试了一个纯 Ruby 实现,完全没有内存泄漏,也不需要 release 调用。
我发现“How do I prevent memory leak when I load large pickle files in a for loop?”关于 Python 循环中的内存泄漏,但在 readBigBinaryFile 中的 while block 开头执行 abuffer=nil 的公认解决方案不起作用。
这是 RubyMotion 的自动内存管理中的错误,还是预期的? 最重要的是,如何在不增加 RubyMotion 应用程序内存使用量的情况下循环读取大文件?
【问题讨论】:
-
我建议阅读“How to Ask”,尤其是指向catb.org/esr/faqs/smart-questions.html 的链接。 “minimal reproducible example”指定您的示例应用程序必须在问题本身中。指向其他网站的链接很容易腐烂然后中断,从而使将来的问题对其他人毫无价值。
-
外部链接是一种商品,希望问题本身嵌入代码,使问题变得完整,并且现在和以后对其他人有用
标签: ruby cocoa memory-management memory-leaks rubymotion