# Allocate particular amount of memory to a ruby thread?

**URL:** <https://www.ruby-forum.com/t/allocate-particular-amount-of-memory-to-a-ruby-thread/140052>\
**Category:** Ruby\
**Created:** [June 10, 2008, 1:46pm UTC](https://www.ruby-forum.com/t/allocate-particular-amount-of-memory-to-a-ruby-thread/140052 "2008-06-10T13:46:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [June 10, 2008, 1:46pm UTC](https://www.ruby-forum.com/t/allocate-particular-amount-of-memory-to-a-ruby-thread/140052/1 "2008-06-10T13:46:35Z")

</div>

how to allocate particular amount of memory to a ruby thread or to a  
ruby script?

for example suppose if i want to start a thread it should take 1 MB  
memory only

Thread.start(memory=1024_1024_1024) do |t|

# some stuff here

end

is there anyway to do like this ?

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [June 10, 2008, 4:50pm UTC](https://www.ruby-forum.com/t/allocate-particular-amount-of-memory-to-a-ruby-thread/140052/2 "2008-06-10T16:50:28Z")

</div>

On Tue, Jun 10, 2008 at 13:46, Pokkai D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> is there anyway to do like this ?

AFAIK, the memory is shared among threads, no matter whether system or  
ruby’s.  
The memory for the whole ruby process/script is allocated on demand,  
with some initial amount.  
This amount and growing ratio is compiled-in (at least for 1.8).

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [June 10, 2008, 8:16pm UTC](https://www.ruby-forum.com/t/allocate-particular-amount-of-memory-to-a-ruby-thread/140052/3 "2008-06-10T20:16:08Z")

</div>

On 10.06.2008 16:48, Jano S. wrote:

> > end
> > 
> > is there anyway to do like this ?
> 
> AFAIK, the memory is shared among threads, no matter whether system or ruby’s.

Exactly. That’s the whole point of threads.

> The memory for the whole ruby process/script is allocated on demand,  
> with some initial amount.  
> This amount and growing ratio is compiled-in (at least for 1.8).

And there is no way to limit it. It might be possible to limit on a per  
process base (depending on OS, see “ulimit -a”) but not on a per thread  
base. And there might be a limit on the stack size. But it does not  
make sense to limit memory allocated by a thread because the memory is  
shared and can be used from any thread.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![mokkai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c2a13f/32.png) [@mokkai](https://www.ruby-forum.com/u/mokkai)\
**Post date:** [June 11, 2008, 2:12am UTC](https://www.ruby-forum.com/t/allocate-particular-amount-of-memory-to-a-ruby-thread/140052/4 "2008-06-11T02:12:07Z")

</div>

On Tue, Jun 10, 2008 at 20:14, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Exactly. That’s the whole point of threads.
> 
> > The memory for the whole ruby process/script is allocated on demand,
> 
> And there is no way to limit it. It might be possible to limit on a per

Pokkai, what’s your primary goal/problem? Why do you want to limit the  
memory?  
Maybe there’s a solution if you can look at the problem from another  
angle.

J.
