From: Micha Feigin <michf@post.tau.ac.il>
To: linux-c-programming <linux-c-programming@vger.kernel.org>
Subject: Re: Newbie question on malloc()
Date: Fri, 4 Jun 2004 15:31:46 +0300 [thread overview]
Message-ID: <20040604123146.GL2562@luna.mooo.com> (raw)
In-Reply-To: <16575.55910.854396.740084@cerise.nosuchdomain.co.uk>
On Fri, Jun 04, 2004 at 03:11:50AM +0100, Glynn Clements wrote:
>
> Micha Feigin wrote:
>
> > > Note that free() doesn't generally return memory to the OS; it usually
> > > just records the fact that the specified region of memory is available
> > > for re-use by subsequent calls to malloc() etc.
> >
> > That would depend on the memory block size. Small memory blocks are
> > held as malloc allocates whole pages at a time, large memory blocks are
> > returned. Depending on size the OS may then also store them on caches
> > in case a memory block of the same size is requested again.
>
> That's what I meant by "generally".
>
> Some implementations *may* return freed memory to the OS. GNU libc 2.x
> does so for blocks above a certain size (the default is 128kb,
> configurable via environment variables and mallopt()).
>
> However, there may also be a limit on the number of such blocks (due
> to kernel limits on the number of memory mappings), so you can't
> drastically reduce the threshold.
>
> Other implementations may or may not provide such a feature.
>
> Also, kernel patches are available which disable this feature. Memory
> which is obtained from anonymous mmap() (rather than brk()) isn't
> restricted by setrlimit(RLIMIT_DATA), which makes it difficult to
> enforce resource limits.
>
> > Otherwise in a long running program that allocates a lot of memory on
> > the start and then frees most of it would still be hogging all that
> > memory.
>
> That may still happen if the memory is allocated in many small chunks
> rather than one large chunk.
>
It may be close to the truth if the program allocates a lot of small
chunks of variable sizes. IIRC with malloc (on glibc) for each chunk
size for small sizes malloc will allocate a page (or group of pages)
and split them up to equal sized chunks.
Different sized chunks sit on different such chains.
This can take more memory but saves on fragmentation and easier
maintenance. For efficiency it may store the last free chunk or so
(don't remember) as cache, but it should store all freed memory (the
doubling technique for dynamic buffers). You add more memory if you
need more then you have, and free some up if you drop bellow a
threshold.
It would be grossly inefficient on system resources otherwise on not
much more efficient in time. Programs that need a memory cache should
handle it themselves.
> --
> Glynn Clements <glynn.clements@virgin.net>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>
> +++++++++++++++++++++++++++++++++++++++++++
> This Mail Was Scanned By Mail-seCure System
> at the Tel-Aviv University CC.
>
next prev parent reply other threads:[~2004-06-04 12:31 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-06-02 11:48 Newbie question on malloc() Wen Guangcheng
2004-06-02 17:08 ` John T. Williams
2004-06-02 17:41 ` Glynn Clements
2004-06-02 17:52 ` John T. Williams
2004-06-03 7:41 ` Glynn Clements
2004-06-03 11:32 ` Micha Feigin
2004-06-04 2:11 ` Glynn Clements
2004-06-04 12:31 ` Micha Feigin [this message]
2004-06-02 18:37 ` Jan-Benedict Glaw
2004-06-03 1:34 ` Micha Feigin
2004-06-03 19:42 ` Jan-Benedict Glaw
2004-06-03 23:44 ` Micha Feigin
2004-06-04 8:06 ` Jan-Benedict Glaw
2004-06-02 17:18 ` Glynn Clements
2004-06-03 1:28 ` Micha Feigin
2004-06-03 7:23 ` Luciano Moreira - igLnx
2004-06-03 7:59 ` Glynn Clements
2004-06-03 22:25 ` John T. Williams
2004-06-03 23:24 ` Paul Gimpelj
2004-06-04 0:14 ` John T. Williams
2004-06-04 2:35 ` Glynn Clements
2004-06-03 23:53 ` Glynn Clements
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20040604123146.GL2562@luna.mooo.com \
--to=michf@post.tau.ac.il \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).