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

···

--
Posted via http://www.ruby-forum.com/.

    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

···

On Fri, Sep 24, 2010 at 7:36 AM, Amit Tomar <amittomer25@yahoo.com> wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Robert Klemme wrote:

···

On Fri, Sep 24, 2010 at 7:36 AM, Amit Tomar <amittomer25@yahoo.com> > 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
--
Posted via http://www.ruby-forum.com/\.

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.

···

On Fri, Sep 24, 2010 at 10:31 AM, Amit Tomar <amittomer25@yahoo.com> wrote:

Robert Klemme wrote:

On Fri, Sep 24, 2010 at 7:36 AM, Amit Tomar <amittomer25@yahoo.com> >> 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

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 Fri, Sep 24, 2010 at 10:31 AM, Amit Tomar <amittomer25@yahoo.com> wrote:

Robert Klemme wrote:

On Fri, Sep 24, 2010 at 7:36 AM, Amit Tomar <amittomer25@yahoo.com> >> 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?

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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Jesús Gabriel y Galán 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

···

On Fri, Sep 24, 2010 at 10:31 AM, Amit Tomar <amittomer25@yahoo.com> > wrote:

--
Posted via http://www.ruby-forum.com/\.

Robert Klemme wrote:

···

On Fri, Sep 24, 2010 at 10:31 AM, Amit Tomar <amittomer25@yahoo.com> > 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???
--
Posted via http://www.ruby-forum.com/\.

sorry its :x_sendfile=> true

···

--
Posted via http://www.ruby-forum.com/.

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.

···

On Sep 24, 2010, at 01:58 , Amit Tomar wrote:

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

Ryan Davis wrote:

···

On Sep 24, 2010, at 01:58 , Amit Tomar 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???
--
Posted via http://www.ruby-forum.com/\.

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 Fri, Sep 24, 2010 at 10:41 PM, Amit Tomar <amittomer25@yahoo.com> wrote:

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

--
Tony Arcieri
Medioh! A Kudelski Brand

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.

···

On 9/24/2010 10:41 PM, Amit Tomar 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 Sat, Sep 25, 2010 at 6:41 AM, Amit Tomar <amittomer25@yahoo.com> wrote:

Ryan Davis wrote:

On Sep 24, 2010, at 01:58 , Amit Tomar 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???

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 Wright

···

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

On 9/24/2010 10:41 PM, Amit Tomar 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 Hoops wrote:

···

On 9/24/2010 10:41 PM, Amit Tomar 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 ???
--
Posted via http://www.ruby-forum.com/\.

Jesús Gabriel y Galán wrote:

···

On Sat, Sep 25, 2010 at 6:41 AM, Amit Tomar <amittomer25@yahoo.com> > 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??
--
Posted via http://www.ruby-forum.com/\.

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 Sep 25, 2010, at 22:44 , Amit Tomar 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??

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

···

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

Walton Hoops wrote:

On 9/24/2010 10:41 PM, Amit Tomar 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 ???

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 Sun, Sep 26, 2010 at 9:53 AM, Ryan Davis <ryand-ruby@zenspider.com> wrote:

On Sep 25, 2010, at 22:44 , Amit Tomar 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.