ファイルから大きな文字列を読み込んで末尾に新たな文字列を追加しようとすると「 failed to allocate memory (NoMemoryError)」がでる。
環境はWindows 7(64bit)、Windows XP、ruby 1.9.3p194 (2012-04-20) [i386-mingw32]で確認。
Debian 6.0.5、ruby 1.9.2p0 (2010-08-18 revision 29036) [i486-linux]では起こらず。
big_string = 'A' * 500000000 big2 = big_string + 'A' # success File.open('test.dat','wb'){|f| f.write(big_string) } File.open('test.dat','rb'){|f| input_str = f.read big3 = input_str + 'A' # failed to allocate memory (NoMemoryError) }
もっと簡単なコード。ファイルは関係なかった。単にメモリまわりがmingw32とlinuxでは違ってるのかな?
big_string = 'A' * 500000000 big2 = big_string + 'A' # success big_string2 = 'B' * 500000000 big3 = big_string2 + 'B' # failed to allocate memory (NoMemoryError)
文字列を空にすれば通った。
big_string = 'A' * 500000000 big2 = big_string + 'A' # success big2 = '' big_string = '' big_string2 = 'B' * 500000000 big3 = big_string2 + 'B' # success