Usenet comp.unix.osf.osf1 (1992-2004)
 help / color / mirror / Atom feed
* strange malloc -- fragmentation ?
@ 1994-02-23  4:22 Ignotum per ignotius
  1994-02-23 13:39 ` Jim Gettys
  0 siblings, 1 reply; 3+ messages in thread
From: Ignotum per ignotius @ 1994-02-23  4:22 UTC (permalink / raw)



DEC ALPHA 3000/400 OSF1.3(a)


I wonder if anyone can clear up a point I that has been bothering me.
I am allocating memory to a large number of arrays. I know the maximum
size of each array in advance & previously used this to malloc some
space in advance. To save space I then decided to calculate the true size,
and allocate only what's needed. This ought to give a 50% saving but 
actually falls well short of this. I guage this by the RSS increase 
after the malloc's and by the maximum size of the process I can run on the
machine. That IBM RS6000 I just experimented with has no such difficulties !

What's happening, is it an alignment/fragmentation question or something
else. Is there a simple fix ?  This is not a time-critical element of my
code.

In hope,

Louis....


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: strange malloc -- fragmentation ?
  1994-02-23  4:22 strange malloc -- fragmentation ? Ignotum per ignotius
@ 1994-02-23 13:39 ` Jim Gettys
  1994-02-23 23:17   ` John Hascall
  0 siblings, 1 reply; 3+ messages in thread
From: Jim Gettys @ 1994-02-23 13:39 UTC (permalink / raw)


In article <2kelld$snu@gap.cco.caltech.edu>, sancho@quixote.gps.caltech.edu (Ignotum per ignotius) writes:
> 
> DEC ALPHA 3000/400 OSF1.3(a)
> 
> 
> I wonder if anyone can clear up a point I that has been bothering me.
> I am allocating memory to a large number of arrays. I know the maximum
> size of each array in advance & previously used this to malloc some
> space in advance. To save space I then decided to calculate the true size,
> and allocate only what's needed. This ought to give a 50% saving but 
> actually falls well short of this. I guage this by the RSS increase 
> after the malloc's and by the maximum size of the process I can run on the
> machine. That IBM RS6000 I just experimented with has no such difficulties !
> 
> What's happening, is it an alignment/fragmentation question or something
> else. Is there a simple fix ?  This is not a time-critical element of my
> code.

The malloc in DEC OSF 1.2 and 1.3 is a Berkeley power-of-two allocator (as I 
remember), and if you happen to allocate near a power of two number of bytes,
rounds up to the next power of two, causing a near factor of two of memory usage
for allocations of this size.  This algorithm is very fast.

OSF 2.0 and later implement a new malloc algorithm, which on average is as fast
as the old algorithm, but as memory efficient as the (typically) slower algorithms
found on other vendor's systems. John Detreville at Digital's Systems Research 
Center did alot of work developing and tuning it.  You get your cake and 
can eat it too.

Remember however, that if your arrays have pointers, you may be needing more
memory for them (64 bits rather than 32 bits).  This won't change.

So the answer is "fixed in 2.0".
			- Jim

-- 
Digital Equipment Corporation
Cambridge Research Laboratory


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: strange malloc -- fragmentation ?
  1994-02-23 13:39 ` Jim Gettys
@ 1994-02-23 23:17   ` John Hascall
  0 siblings, 0 replies; 3+ messages in thread
From: John Hascall @ 1994-02-23 23:17 UTC (permalink / raw)


Jim Gettys <jg@crl.dec.com> wrote:
}sancho@quixote.gps.caltech.edu (Ignotum per ignotius) writes:
}> DEC ALPHA 3000/400 OSF1.3(a)
   [...malloc grows program RSS more than expected...]
}> What's happening, is it an alignment/fragmentation question or something
}> else. Is there a simple fix ?  This is not a time-critical element of my
}> code.

}The malloc in DEC OSF 1.2 and 1.3 is a Berkeley power-of-two allocator (as I 
}remember), and if you happen to allocate near a power of two number of bytes,
}rounds up to the next power of two, causing a near factor of two of memory
}usage for allocations of this size.  This algorithm is very fast.

  As a matter of fact, if it is like the Ultrix malloc, the very worst
  size to malloc is any power of 2 less or equal to 4096 (the page size).
  The best (least wasteful) size is (2^n - 4).

  This malloc always grabs core in pagesize chunks.  For sizes < pagesize,
  the chunk is divided into equal parts.  For example, when the "512 byte"
  bucket needs more entries, 4096 bytes are grabbed with sbrk and divided
  into 8 512 bytes blocks.  Blocks in this bucket are used to satisfy
  requests from 256(253) to 508 bytes, as 4 bytes at the front are
  used for housekeeping.  For buckets 4K and bigger, the lossage is upto
  1 page (so the returned chunk is always page-aligned).

John
-- I present the following for your horror and amazement (it saved 7MB core) --

#ifdef  BLASPHEMOUS_MALLOC
/*
 * The size of these hash tables is always a power of 2,
 * so guess which class of sizes are most wastefully allocated
 * by Ultrix's malloc() -- good guess...
 *
 * We do some blasphemous malloc twiddling here...
 */
union overhead {
        union overhead *        ov_next;        /* when free */
        struct {
                unsigned char   ovu_magic;      /* magic number */
                unsigned char   ovu_index;      /* bucket # */
        } ovu;
};
#define ov_magic        ovu.ovu_magic
#define ov_index        ovu.ovu_index
#define MAGIC           0xef            /* magic # on accounting info */

HashEntry ** hashAllocTable(size)
        unsigned int    size;
{
        register unsigned int   bytes   = size * sizeof(caddr_t);
        union overhead *        new;

        if (bytes <= sizeof(union overhead)) return (NULL);
        new = (union overhead *)malloc(bytes - sizeof(union overhead));
        if (new == NULL) return (NULL);
        return ((HashEntry **)new - 1);
}

hashFreeTable(table, size)
        HashEntry **    table;
        unsigned int    size;
{
        register int            bytes   = size * sizeof(caddr_t);
        register int            bucket;
        union overhead *        old;

        if (table == NULL) return;
        bucket = -3;
        while ((bytes >>= 1) > 0) ++bucket;
        if (bucket < 0) return;
        if (bucket == 10) bucket = 9;           /* correct anomaly */
        old = (union overhead *)table;
        old->ov_magic = MAGIC;
        old->ov_index = bucket;
        free(old + 1);
}
#else
	/* ... normal way ... */
#endif
----------------------------------------------------------------------------
-- 
John Hascall                   ``An ill-chosen word is the fool's messenger.''
Systems Software Engineer
Project Vincent
Iowa State University Computation Center  +  Ames, IA  50011  +  515/294-9551


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~1994-02-23 23:17 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1994-02-23  4:22 strange malloc -- fragmentation ? Ignotum per ignotius
1994-02-23 13:39 ` Jim Gettys
1994-02-23 23:17   ` John Hascall

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox