On Wed, Aug 21, 2002 at 03:50:40AM +0900, William Djaja Tjokroaminata wrote:
> Reimer Behrends <behrends / cse.msu.edu> wrote:
> =================================================================
> > A problem occurs if, say, you have an Image class that allocates memory
> > for the pixel data using malloc():
> 
> > for i = 1..1000 do
> >   img = Image.load(directory + i.to_s + ".png")
> > end
> 
> > Assuming that each image occupies, say, 1 MB in memory, the program
> > allocates roughly 1 GB of memory. Yet as far as Ruby knows, only a few
> > kilobytes have been requested, so no garbage collection is initiated
> > and the program starts chewing through all the available swap space,
> > even though there is plenty of garbage to collect.
> =================================================================
> 
> The 1 GB memory is a valid memory, isn't it?  So we should not have a
> problem with it, unless, of course, our computer does not have that much
> of memory.  I just doubt your last sentence above, "even though there is
> plenty of garbage to collect".  With GC_MALLOC_LIMIT of 8000000 bytes,
> isn't that the maximum garbage to collect is 8 megabytes?

void *
ruby_xmalloc(size)
    long size;
{
    void *mem;

    if (size < 0) {
        rb_raise(rb_eNoMemError, "negative allocation size (or too big)");
    }
    if (size == 0) size = 1;
    malloc_memories += size;

    if (malloc_memories > GC_MALLOC_LIMIT) {
        rb_gc();
    }
 ....

This means the garbage collector will automatically be called after
GC_MALLOC_LIMIT (8MB on sane platforms) bytes have been ALLOCated
(malloc_memories is reset afterwards).
But if the data you ALLOCated points to malloc()'ed data there will
be indeed a lot of garbage, and Ruby doesn't know it.

-- 
 _           _                             
| |__   __ _| |_ ___ _ __ ___   __ _ _ __  
| '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ 
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
	Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com
  
How should I know if it works?  That's what beta testers are for.  I
only coded it.
	-- Attributed to Linus Torvalds, somewhere in a posting