I was coding some C when I started asking myself about free() inside a shared library and SIGKILL, and so on. Then, after some reading and re-thinking, I came up to this simple question.

CONTEXT : When the loader reads an ELF file that points to some libA.so shared object, and it loads the library, will it map the entire segments onto the program's memory address space? will it assign it as shared memory?

As far as I know, the kernel will not take shared memory of dead processes, so:

  1. What happens if a shared object was loaded, and it allocates memory, and then the main program receives SIGKILL? Will the allocated memory be considered shared memory and be a leak?
  2. What happens if I have two programs loading the same shared library, and one of them dies without freeing?

Both ended up as: Is the shared library object loaded as shared memory for the program?

My question is quite generic. I would love to compare different kernels / loaders.


Since all modern OS follow the basic concepts from SunOS-4.0 (1988) and since they are even based on the code from SunOS (Sun offered the sources in the early 1990 to FreeBSD from where is has been copied...) there is not much that differs.

The shared library file is mapped into the programs memory and this is done in a shared way with copy on write for the data, so most of the needed RAM is shared.

  • malloc() is not called by "the library" but by a process, the related memory is associated to the process

  • Signals are sent to the process - not to a library

  • If a program/process is terminated, the malloc'd memory is freed by the kernel and if the reference count to the mapped library goes to zero, then the whole library is removed from the memory.

  • ... but it’s not a shared mapping which avoids the behaviour the OP is asking about in point 1. – Stephen Kitt Jun 20 '18 at 7:43
  • So, if the library has no shared memory allocation code such as mmap, it's mallocd memory counts as process memory, is that true? – D4RIO Jun 20 '18 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.