But each duplicate has to be swapped out individually; and if they're
swapped in, each one gets a different page of memory even though they're
all the same.
Yep. Watch out the sky is falling. Its amazing that anybody can get
anything to run without string literals unless they have a supercomputer at
their disposal.
Note that you ignored the main question as to whether all of this memory
usage is really significant enough to justify the costs.
And doesn't generally need that control.
You are one who is so concerned about efficiency and resource usage. Given
that, you should see the advantage that the application has in optimizing
its own memory usage based on the knowledge of its own usage. Personally,
I have not found a situation where it matters enough to be significant.
Provided that the module was written in such a way that no references to
it linger. This isn't entirely within the author's control; the module
may need to use libraries which retain references.
Presumably, any module that you would load dynamically would need to be
self contained enough to be unloaded; otherwise, this discussion is rather
moot. The problem would not deal with the subject at hand.
Executables and libraries are paged in as needed. If the data is never
accessed, it won't get paged in. If it's read-only data, even if it
does get paged in, there will never be more than one copy in RAM.
Memory is paged in blocks (usually 4k for i386). If I only need 255
characters in a given block, the memory paged in will still be 4k
irregardless of the fact that I am only using 255 bytes.
Only by the same process. If you have multiple instances of the program,
each one will have their own copy the data. If the program uses that data
regularly, each instance will have its own copy in RAM, because the kernel
can't tell that they're all the same (and in practice, they may all be
slightly different, i.e. memory locations will differ).
If you are so worried about it, for the case of forking daemons, you can
use shared memory. I have not come across a situation where the
duplification was significant enough to cause problems.
You can achieve the same results using files which are mmap()d read-only.
These can either be shared libraries, or a custom format.
For all I know, that may be how gettext works under the hood; and, I
wouldn't treat mmaped segments as any more stable then localized variables
or dynamically loaded libraries -- they too can by munmap[ed]().
So yes, I concede that Nobody is the god of resource usage; and, its
amazing that I have able to get along all these years without passing
string litterals or a cluster of supercomputers for my programs to run on.
He is duly entitled to wank off for every spare byte that he saves.
Meanwhile, I will continue to strive to create solid, safer, code even
if it means that I use a few more resources (which can be mitigated
if necessary). I sure that I am going down a bad road where the only
possible conclusion is insufficient memory. I guess you should pity me
and my demise ... or something.