Static linking and memory usage

Oliver Fromme olli at lurza.secnetix.de
Tue Aug 14 10:37:19 PDT 2007


Jeremy Chadwick wrote:
 > Pete French wrote:
 > > If I have an executable which is staticly linked with many copies
 > > of it running (a CGI script in fact), will all those copies share
 > > the text segment of the executable on the disc, or will it actually
 > > use up real memory resource with many copies of the executable ? I
 > > have been assuming the former, but possibly that is not correct.
 > 
 > How I understand it:
 > 
 > A statically-linked binary contains portions/copies of the text, data,
 > and bss segments of .a library.  These are part of the actual binary
 > itself, thus become part of the memory space of the program when run.
 > Only the text segment gets shared; the data and bss segments, as far as
 > I know, do not get shared.

Upon a fork() the data and bss segments are also shared,
but marked copy-on-write.  That is, as soon as the child
tries to modify something, a copy of the page is made and
assigned to the child process in place of the parent's
page.

Of course, unrelated processes (i.e. which ave no parent-
child relationship) never share data segments.

 > With a statically-linked binary, when the binary forks, the text segment
 > is kept intact between the parent and child -- that is, there is only 1
 > in-memory copy of that segment.
 > 
 > However, *different parents* will have their own copy of that library in
 > their memory space.

Are you sure?  I _think_ (but I'm not 100% sure) that the
mapped text pages of a binary are associated with the
vnode of the executable file, but not with a particular
process.  That means that unrelated processes running
from the same binary file (vnode) will share the same
text pages in memory.

Please, someone correct me if I'm wrong.

Best regards
   Oliver

-- 
Oliver Fromme, secnetix GmbH & Co. KG, Marktplatz 29, 85567 Grafing b. M.
Handelsregister: Registergericht Muenchen, HRA 74606,  Geschäftsfuehrung:
secnetix Verwaltungsgesellsch. mbH, Handelsregister: Registergericht Mün-
chen, HRB 125758,  Geschäftsführer: Maik Bachmann, Olaf Erb, Ralf Gebhart

FreeBSD-Dienstleistungen, -Produkte und mehr:  http://www.secnetix.de/bsd

"Emacs ist für mich kein Editor. Für mich ist das genau das gleiche, als
wenn ich nach einem Fahrrad (für die Sonntagbrötchen) frage und einen
pangalaktischen Raumkreuzer mit 10 km Gesamtlänge bekomme. Ich weiß nicht,
was ich damit soll." -- Frank Klemm, de.comp.os.unix.discussion


More information about the freebsd-stable mailing list