On Thursday 11 January 2007 14:26, "b.n." <brullonulla@...> wrote
about 'Re: [gentoo-desktop] CD player recommendations ??':
> Uwe Thiem ha scritto:
> > On 11 January 2007 10:38, JT Justman wrote:
> >> b.n. wrote:
> >>> I wasn't aware of this, I must confess. Is there any way to prevent
> >>> it, so that when you close the app, it cleans the libs from memory?
> >> Not sure how the cleanup works, but these things will just get paged
> >> off to swap if you aren't using them. I wouldn't worry about it
> >> unless swap is at a premium.
> > As soon as a library isn't used by any application any more, the
> > memory it occupies is freed. Of course, it might still be cached - but
> > that doesn't hurt at all.
> Now, who is right? :)
At dynamically-linked binary startup, or in response to a dlopen call, a
library will be mmap()ed into process memory space. This does not
necessarily load the library from disk, although it would indicate to the
I/O scheduler that operations are about to happen to/from that disk space.
Most of the library will be mapped read-only and thus shared. Any pages
that the library uses to keep state, will be mapped copy-on-write (and
generally noexec). The symbol table will also need to be read, which will
cause some reads from disk unless that area of the library is already
cached. As pages containing functions or data are needed and not found in
cache, they will also be loaded. If pages go unused, they may also be
swapped out as needed. [I'm a little fuzzy here as to weather a read-only
page is ever written to actual swap space, the kernel may just remember
where (on disk) the original is keep and release the real memory page.]
If the library was opened with dlopen() an appropriate call to dlclose()
will cause the library to be munmap()ed, but the library may still remain
in cache. As with all mmap()ings, when the process dies the memory is
(effectively) munmap()ed. In either munmap() case, the read-only pages of
the library may stay in active memory or swap memory if opened by another
process, and may stay in cache even if no other process is currently using
the them. The copy-on-write pages will be freed (from either active or
swap) when munmap()ed.
At least, that's the gist of it. There's a lot there that I've glossed
over, but it should make memory management of shared libraries make a
"If there's one thing we've established over the years,
it's that the vast majority of our users don't have the slightest
clue what's best for them in terms of package stability."
-- Gentoo Developer Ciaran McCreesh