组合两个具有 100k 项的哈希数组会导致在 2GB 虚拟机上运行的进程耗尽内存。我不明白如何/为什么。
假设我有一个像这样的哈希,我用 50 个键/值对填充它。
h = {}
1..50.times{ h[SecureRandom.hex] = SecureRandom.hex}
我将 100k h
s 放入两个数组中,如下所示:
a = []
a1 = []
1..100_000.times{ a << h }
1..100_000.times{ a1 << h }
当我尝试添加时a1
,a
IRB 内存不足:
2.1.1 :008 > a << a1
NoMemoryError: failed to allocate memory
这两个数组真的太大而无法在内存中组合吗?实现此目的的首选方法是什么?
我正在运行ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-linux]
,VM 上没有运行其他进程。