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: Thu, 3 Jun 2004 14:32:41 +0300 [thread overview]
Message-ID: <20040603113241.GH2562@luna.mooo.com> (raw)
In-Reply-To: <16574.54829.111750.764395@cerise.nosuchdomain.co.uk>
On Thu, Jun 03, 2004 at 08:41:33AM +0100, Glynn Clements wrote:
>
> John T. Williams wrote:
>
> > > > As I understand it, that is entirely up to the operation system. Linux
> > > > and NT Kernel systems do reclaim unfreed memory, however I believe one
> > > > the major problems with Win98 was that it did not.
> > > >
> > > > Anyone with more information feel free to correct me
> > >
> > > I very much doubt that the above is accurate.
> > >
> > > It may be that Win98 had some specific memory leaks, but any OS which,
> > > in the general case, failed to recover a process' memory upon
> > > termination would run out of memory very quickly.
> >
> > What you never had win98 crash on you after running for an hour? ;)
>
> Sure. I've also had it crash after running for less than a minute. And
> I've also had it run for hours without crashing.
>
> However, if it didn't reclaim a process' memory upon termination, the
> scenario where you run a short-lived but memory-hungry program many
> times simply wouldn't be possible.
>
> Bear in mind that many (possibly most) programs don't explicitly free
> dynamically-allocated memory blocks upon termination; they just
> terminate.
>
> This is recommended practice. Explicitly freeing memory blocks is a
> waste of time: you're basically just modifying regions of the process'
> memory, which is about to be returned to the OS, whereupon its
> contents become irrelevant.
>
> [It may also make the program substantially more complex, as you would
> need to keep track of such blocks.]
>
> 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.
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.
As for not freeing memory on program exit thats another issue since you
are saving malloc's internal work (which especially with large memory
chunks will probably mostly be a system call letting the kernel do most
of the work, for small chunks there is much more overhead).
> Similarly, malloc() doesn't necessary result in any additional memory
> being obtained from the OS; frequently it will just return a region of
> memory which has already been allocated to the process but isn't being
> used.
>
> > May statement is based on something I remember vaguely from my OS class
> > 2 years ago, so I'm quite happy to believe I'm wrong. I remember there
> > was some major issue with how win98 handled dynamic allocated memory,
> > but I can't exactly remember what.
>
> The main reason for the unreliability of Win95/98/ME is that, for
> compatibility with earlier versions, certain regions of memory are
> unprotected. So, if an application crashes, it can corrupt data
> belonging to other applications or the OS.
>
> [The details relate to the fact that the 8086 doesn't have an MMU, so
> there is a single, global address space shared by all processes (and
> the OS). Originally, interprocess communication made extensive use of
> passing around handles to memory blocks, relying upon the fact that
> any process could read (and write) any part of memory. While Windows
> 3.1 and later require at least a 286, the behaviour was retained for
> compatibility.]
>
> > After googling a little I seems that win95 had some problems with
> > Garbage Collection not win98.
>
> The differences between 95 and 98 are essentially superficial. Win98
> includes IE, and probably a significant number of bug fixes, which may
> include bugs in memory allocation. However, the underlying
> architecture is unchanged, and that includes the way in which memory
> management works (other than any outright bugs which may have been
> fixed).
>
> --
> 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-03 11:32 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 [this message]
2004-06-04 2:11 ` Glynn Clements
2004-06-04 12:31 ` Micha Feigin
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=20040603113241.GH2562@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).