How do i force ruby to release memory

Hi all,
I would like to know is there any way to ask ruby to release
memory.
because for me its not happening automatically…garbase collection

On Fri, Sep 24, 2010 at 7:36 AM, Amit T. [email protected]
wrote:

    I would like to know is there any way to ask ruby to release

memory.

There isn’t.

because for me its not happening automatically…garbase collection

How do you know? How do you know that Ruby is the cause and not an
application code memory leak?

Cheers

robert

Robert K. wrote:

On Fri, Sep 24, 2010 at 7:36 AM, Amit T. [email protected]
wrote:

? ? ? ? I would like to know is there any way to ask ruby to release
memory.

There isn’t.

because for me its not happening automatically…garbase collection

How do you know? How do you know that Ruby is the cause and not an
application code memory leak?

Cheers

robert

Are you saying my appliaction causing memory leak…
or if it is so how do i tell my application to free memory…
is there any specific way to do it

On Fri, Sep 24, 2010 at 10:31 AM, Amit T. [email protected]
wrote:

How do you know? How do you know that Ruby is the cause and not an
application code memory leak?

Cheers

robert

Are you saying my appliaction causing memory leak…
or if it is so how do i tell my application to free memory…
is there any specific way to do it

The Garbage Collector collects garbage, meaning objects which are not
referenced anymore.
If you are keeping references to objects, those objects will never be
garbage collected.

In order to fix this, you have to identify which objects you think
should be garbage collected but aren’t, and then checking which
references to those objects are keeping them from being freed. Then
fix the code.

If you have a piece of code that shows the issue we might be able to
help.

Jesus.

sorry its :x_sendfile=> true

Jesús Gabriel y Galán wrote:

On Fri, Sep 24, 2010 at 10:31 AM, Amit T. [email protected]
wrote:

How do you know? ?How do you know that Ruby is the cause and not an
application code memory leak?

Cheers

robert

Are you saying my appliaction causing memory leak…
or if it is so how do i tell my application to free memory…
is there any specific way to do it

The Garbage Collector collects garbage, meaning objects which are not
referenced anymore.
If you are keeping references to objects, those objects will never be
garbage collected.

In order to fix this, you have to identify which objects you think
should be garbage collected but aren’t, and then checking which
references to those objects are keeping them from being freed. Then
fix the code.

If you have a piece of code that shows the issue we might be able to
help.

Jesus.

yaa sure Jesus , this is code for download , i downloded 1.1 GB of
stream and trying to download it again but i got failed to allocate
memory .after that seen memory used by ruby application from task
manager an my ruby application still holding memory

def downlaod
@stream = Stream.find(params[:id])

send_file(@stream.location,:filename => @stream.name,:disposition
‘attachment’,:buffer_size => 4096,:x_sendfile :true)

end

Robert K. wrote:

On Fri, Sep 24, 2010 at 10:31 AM, Amit T. [email protected]
wrote:

How do you know? ?How do you know that Ruby is the cause and not an
application code memory leak?

Are you saying my appliaction causing memory leak…
or if it is so how do i tell my application to free memory…
is there any specific way to do it

I am not saying anything - we don’t even have a proper problem
description. Are you wondering why your memory consumption goes up
all the time? Are you wondering why the process does not give back
memory to the operating system although you have released all
instances?

Cheers

robert

robert ,i would like to know how i force instances to just free memory
i posted my code above…
could you please help me out from this situation???

On Fri, Sep 24, 2010 at 10:31 AM, Amit T. [email protected]
wrote:

How do you know? How do you know that Ruby is the cause and not an
application code memory leak?

Are you saying my appliaction causing memory leak…
or if it is so how do i tell my application to free memory…
is there any specific way to do it

I am not saying anything - we don’t even have a proper problem
description. Are you wondering why your memory consumption goes up
all the time? Are you wondering why the process does not give back
memory to the operating system although you have released all
instances?

Cheers

robert

On Sep 24, 2010, at 01:58 , Amit T. wrote:

def downlaod
@stream = Stream.find(params[:id])

right there… you’re creating an instance variable that is holding onto
your Stream object. If that doesn’t let go of the 1.1g it downloaded,
then you’ll hold onto it forever.

Ryan D. wrote:

On Sep 24, 2010, at 01:58 , Amit T. wrote:

def downlaod
@stream = Stream.find(params[:id])

right there… you’re creating an instance variable that is holding onto
your Stream object. If that doesn’t let go of the 1.1g it downloaded,
then you’ll hold onto it forever.

but Ryan how do i force it to release memory???

On Fri, Sep 24, 2010 at 10:41 PM, Amit T. [email protected]
wrote:

but Ryan how do i force it to release memory???

Remove any references to an object which is using a particular piece of
memory. Then it will eventually get garbage collected.

If you need more advice than that, you’re doing it wrong. I’m sure there
are
a lot of “that guy” type people out there who might point out the
various
Ruby GC methods, but if you have to use them, you’re probably doing it
wrong, and also they’re not really portable to Ruby implementations
beyond
MRI and are mostly hacks for the crappy garbage collection available in
MRI/YARV.

On Sep 25, 2010, at 1:20 AM, Walton H. wrote:

It is also important to realize that the memory will not be released
back to the OS but instead will just become available for re-use by
Ruby.

In general there is no way to arrange for Ruby (and most other
languages)
to return memory to the OS. It is difficult to arrange for a contiguous
block of free memory. Live objects tend to be scattered here and there
breaking up any contiguous blocks of free memory.

If anyone knows of some good examples of language runtimes that do
manage to return memory to the OS, I’d be curious to hear about them.

Gary W.

On 9/24/2010 10:41 PM, Amit T. wrote:

but Ryan how do i force it to release memory???
You don’t. You remove the reference by setting @stream=nil, and the
Garbage Collector will release the memory when it gets around to it.
Note there are no guarantees about when the GC will run, or what it will
free when it does, so it’s not like you will see the memory drop
immediately when you set @stream to nil, in fact it’s very likely that
it won’t get run until the interpreter tries to allocate the next chunk
of memory for the next 1G object.

Walton H. wrote:

On 9/24/2010 10:41 PM, Amit T. wrote:

but Ryan how do i force it to release memory???
You don’t. You remove the reference by setting @stream=nil, and the
Garbage Collector will release the memory when it gets around to it.
Note there are no guarantees about when the GC will run, or what it will
free when it does, so it’s not like you will see the memory drop
immediately when you set @stream to nil, in fact it’s very likely that
it won’t get run until the interpreter tries to allocate the next chunk
of memory for the next 1G object.

yaa walton , i already tried with @stream=nil but i have same situation
with memory,ruby is still holding memory and also trying to use GC.start
but of no use

What should i do now ???

Jesús Gabriel y Galán wrote:

On Sat, Sep 25, 2010 at 6:41 AM, Amit T. [email protected]
wrote:

but Ryan how do i force it to release memory???
Do you need the stream outside that method? If not, don’t use an
instance variable, and use a local variable instead.

def download
stream = Stream.find(params[:id])

end

When the method finishes executing the stream variable goes out of
scope, and it’s the only reference to that object, so that memory can
be freed by the GC.

Jesus.

i don’t want stream outside this method.
and what you want to say as soon as method finishes it automatically
release
memmory (since stream scope is only within the function)
am i right??

On Sep 25, 2010, at 22:44 , Amit T. wrote:

i don’t want stream outside this method.
and what you want to say as soon as method finishes it automatically
release
memmory (since stream scope is only within the function)
am i right??

no. that’s not how a GC’d environment (usually) works. You can force the
GC to run by telling it to (ri GC for more info), but even that won’t
guarantee your memory gets reclaimed by the system (or at all, since
ruby uses a conservative GC there is no guarantee at all), as others
have said in this thread.

On Sat, Sep 25, 2010 at 6:41 AM, Amit T. [email protected]
wrote:

but Ryan how do i force it to release memory???
Do you need the stream outside that method? If not, don’t use an
instance variable, and use a local variable instead.

def download
stream = Stream.find(params[:id])

end

When the method finishes executing the stream variable goes out of
scope, and it’s the only reference to that object, so that memory can
be freed by the GC.

Jesus.

On Sun, Sep 26, 2010 at 9:53 AM, Ryan D. [email protected]
wrote:

On Sep 25, 2010, at 22:44 , Amit T. wrote:

i don’t want stream outside this method.
and what you want to say as soon as method finishes it automatically
release
memmory (since stream scope is only within the function)
am i right??

no. that’s not how a GC’d environment (usually) works. You can force the GC to run by telling it to (ri GC for more info), but even that won’t guarantee your memory gets reclaimed by the system (or at all, since ruby uses a conservative GC there is no guarantee at all), as others have said in this thread.

But that’s usually the best you can (or should) do: check the objects
that you don’t really want anymore, and remove any reference to them.
After that, as Ryan says, there are no or little guarantees about when
or if the object would be removed.

Jesus.

On 09/26/2010 07:36 AM, Amit T. wrote:

of memory for the next 1G object.

yaa walton , i already tried with @stream=nil but i have same situation
with memory,ruby is still holding memory and also trying to use GC.start
but of no use

What should i do now ???

Relax. Did you see NoMemoryError? If not, there’s not more to do here
unless you want to change the library you are using to one that uses
less memory.

Cheers

robert