似乎 MRI 会为每个新线程重复分配内存。
我使用 Ubuntu x64、ruby-2.2.4 (rvm),这是我得到的:
刚开始irb:
I see pmap -d 1656
59760K (allocated memory, or '[ stack ]' for the program stack [man pmap(1)]) memory usage:
And when creating a thread:
I see pmap -d 1656
127352K memory usage:
So, I see duplication 59760K -> 127352K of memory allocation.
Such behavior is similar to result of the fork()
call, which being used for creation a new process, makes a copy of its calling process data ('copy-on-write' is out this context) for new process.
But Thread is created in the same process and shares its data, and it looks strange...
In practice, it means that Thread in Ruby has similar to Process restriction in memory usage: new thread creation fails when allocated memory getting closer to physical memory size.
I am curious, WHY?
UPDATE
这不是重复内存,而是为每个线程额外分配约 50K。感谢@tadman 建议这是一种开销,而不是像以 fork() 的方式复制内存。