Manual Memory Management and Automatic Garbage Collection

Did you mean to include manual cleanup of particular instances which
are known to be not used any more (e.g. when a method is left)? In
that case keep in mind that it is hard to know which objects can be
collected because you do not know what other objects local variable
hold or create and you also do not know whether references of objects
in local variables are published somewhere else so they have to live
longer than the stack frame where their local variable reference is
stored.

Cheers

robert

···

On Thu, May 19, 2011 at 12:43 AM, Tridib Bandopadhyay <tridib04@gmail.com> wrote:

Robert K. wrote in post #999427:

On Tue, May 17, 2011 at 9:58 PM, Tridib Bandopadhyay >> <tridib04@gmail.com> wrote:
What I do not understand: what is the aim of your research? I mean,
people have spent numerous hours of research and engineering to make
automatic GC work, and you seem to be mainly concerned with adding
manual memory management to a language which has automatic GC. What
is the point of your research?

Yeah i know that.. But since Ruby is Using Stop the World concept. So I
was thinking of not letting the GC to come and free memory every time
for small objects,Stopping the code. So I just want to check if it can
be helpful or not...

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

You could also take a look at ObjectSpace class and its methods, esp. "garbage_collect" and "define_finalizer". But if I'm not mistaken, they won't instantly initialize GC - as it was said earlier, Ruby decides when it's best to do that alone by itself.

Most likely Garbage Collection is not being run for your (rather short) script. Does your message print when you run GC.start()? If so your message is working fine. Try writing something that creates a log of garbage to be collected. I think something like:

i=1
while (true)
   puts "message #{i}"
   i+=1
end

should do the trick. Of course, that particular program will never exit.

···

On 12/06/2010 08:48 PM, Tridib Bandopadhyay wrote:

I shifted the printf statement above the return statement.Here is the
output I am getting--

./ruby doug.rb
Please enter number 1 : 3
Please enter number 2 : 4
Answer : 7
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899-bash-3.00$

Jeremy Bopp wrote in post #967674:

Garbage collector comes for cleaning with a printf statement stating its
work.

Maybe there are optimizations for String objects that circumvent parts
of regular garbage collection. Have you tried looking at the memory
consumption of your ruby process as you run the while loop? Is memory
usage growing over time, or does it plateau at some point?

The plateau is at the same point.Even if I ran the code for at-least
half and Hour.

Another suggestion would be to pick up a template library such as erb
and use that to process some large template files with random data
repeatedly. That should hopefully cause some memory growth with
something more than simple strings.

Sorry..I didn't understand this part..

Regards

Tridib

···

On 12/10/2010 09:37 AM, Tridib Bandopadhyay wrote:

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

Walton Hoops wrote in post #966746:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899-bash-3.00$

Most likely Garbage Collection is not being run for your (rather short)
script. Does your message print when you run GC.start()? If so your
message is working fine. Try writing something that creates a log of
garbage to be collected. I think something like:

i=1
while (true)
   puts "message #{i}"
   i+=1
end

should do the trick. Of course, that particular program will never
exit.

Yes When I can see my message when I run my GC.start().So When does the
garbage collector comes into play?.When we call them or isn't it
automatic after each object declaration it will always come for
cleaning.

But can't I see the message when I am running a program of my own.

Secondly how does the make command do interpret the gc.c file into
*.ext?Is there any mechanism?.

Regards

Tridib

···

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

Use the ERB class:

http://rdoc.info/docs/ruby-stdlib/1.8.7/ERB

Create a handful of files to be used as templates of the form required
for that class. Ensure that the templates have data you can replace.
Process the templates with generated data in a loop.

We're basically making what should hopefully be a non-trivial form of
the while loop you've been using that prints a string with a number
embedded. Hopefully, this will allocate a sufficient number of objects
in a way that will eventually trigger garbage collection. If it doesn't
work, get creative and try to figure out some other way to lots of
non-trivial objects.

-Jeremy

···

On 12/10/2010 10:58 AM, Tridib Bandopadhyay wrote:

Jeremy Bopp wrote in post #967674:

Another suggestion would be to pick up a template library such as erb
and use that to process some large template files with random data
repeatedly. That should hopefully cause some memory growth with
something more than simple strings.

Sorry..I didn't understand this part..

Yes When I can see my message when I run my GC.start().So When does the
garbage collector comes into play?.When we call them or isn't it
automatic after each object declaration it will always come for
cleaning.

Garbage collection is expensive, so the garbage collector's operation is
usually postponed until there is a large enough unit of work to warrant
its cost of operation. The nice thing about this is that the GC doesn't
have to run at all for sufficiently simple scripts. This will make such
scripts much faster than if you run the GC as a result of every object's
allocation.

But can't I see the message when I am running a program of my own.

If you want to see the GC run on its own, you need to cause enough
objects and memory to be allocated to trigger it. I can't personally
say what that trigger is, but it's probably a safe bet that allocating
some large number of objects is one such trigger. Walton's example does
that by creating a new string in each iteration of the while loop.

Secondly how does the make command do interpret the gc.c file into
*.ext?Is there any mechanism?.

I covered this to some extent in another response in this thread.
Obviously, there is a mechanism or you wouldn't get a ruby program to
run with your scripts in the first place. :wink: The C compiler is used
under the covers, and the make program automates the process.

Why do you ask? Do you suspect that your changes are not making it into
your ruby program for some reason?

From the sound of things, you don't really have much of a grasp on the
theory of garbage collection. You might find your task much easier if
you go read up more on the general topic and perhaps some discussions of
particular implementations. You should find plenty of that information
if you search around for Java garbage collection for instance. Without
more of a background in the theory, it's going to take you much more
time to understand Ruby's implementation.

-Jeremy

···

On 12/06/2010 10:54 PM, Tridib Bandopadhyay wrote: