Reputation: 3063
It seems that MRI makes duplication of memory allocation for every new thread.
I use Ubuntu x64, ruby-2.2.4 (rvm), and this what i get:
Just started 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
It's not duplication memory but additional allocation for ~50K for each thread. Thanks @tadman for suggestion that it's an overhead and not something like copying memory in the fork()'s way.
Upvotes: 2
Views: 643