On Wed, 16 May 2012 13:29:48 -0400, David T. Ashley
<
das...@gmail.com> wrote:
>On Tue, 15 May 2012 21:34:57 +0000 (UTC), Raj Pashwar
><
raj1...@hotmail.NOSPAM.com> wrote:
>
>>As per subject-line, thank-you
>
>Generally, it depends on the "division of labor" between the run-time
>library and the operating system.
>
>Some operating systems (in the old days before PCs used hardware
>memory management and all that) had OS function calls that would
>handle small blocks quite efficiently. malloc() and free() were just
>empty wrappers, and dynamically-allocated memory belonging to the OS
>and the application were freely mixed. Some embedded systems may
>still have this kind of division of labor.
>
>Nowadays, on a modern PC, the landscape is different.
>
>No OS is likely to have the capability to hand the run-time library a
>chunk of memory smaller than the page size supported by the memory
>management hardware. I don't know what this figure is nowadays, but
>I'd guess maybe between 256K and 1M. You typically wouldn't be able
>to allocate from the operating system, for example, a 30-byte chunk of
>memory.
The vast majority of systems use 4-64KB pages (and 4KB pages are most
common of all). ARM allowed (in the past) smaller pages. Many
systems also allow large pages, usually by omitting the lowest level
of the page table hierarchy (x86, for example, allows 2MB or 4MB
pages, depending on which format page tables you're using), but these
tend to be used mainly to replace large contiguous allocations of
"normal" sized pages, and particularly for areas some subject to
paging or allocation (for example, the OS areas are fairly commonly
mapped with large pages).
As for OS's allowing small allocations... Obviously you need to
define the OS's boundaries rather carefully. For example Windows
Heap*() functions are defined as part of the OS API, but act more like
what you'd expect malloc() to do (and are implemented largely in user
space - in fact some Windows malloc-like implementations map simply to
HeapAlloc). As another example, zOS has the "GETMAIN" macro as its
primary memory allocation API, which also handles small chunks.
>The run-time library is likely to take these large chunks allocated by
>the operating system and subdivide them so it can hand out smaller
>chunks via malloc().
>
>There is no guarantee that the algorithm used by malloc() and free()
>would allow any memory to be returned to the OS until every block had
>been free()'d.
True.
>There is no reason usually to return memory to the OS until the
>process terminates. Most programs that use dynamic memory use a bunch
>and then terminate ... it wouldn't benefit the average program to
>return anything to the OS until the program terminates ... just adds
>complexity and decreases speed.
That's sometimes true, sometimes not. Consider an application that
lets you load a large file to work on for a while, and then lets you
work on something else. Returning that memory to the OS would only be
polite.
In any event, many implementations do things like handle small
allocations internally (by subdividing larger chunks allocated from
the OS), and large allocations as OS calls. So often freeing a large
allocated chunk actually does return the storage to the OS.
In short, it's impossible to generalize.