3

I have many executables that are linked with tcmalloc (.a). I usually do it at the executable level, so that any shared library loaded by the executable benefits from tcmalloc.

However, I have a scenario where I need to provide a .so library to an user.

Is it ok to use tcmalloc in that shared library?

What happens if the user's executable itself is not linked with tcmalloc?

Thanks.

Uraza
  • 556
  • 3
  • 17
  • If the performance of your library is greatly enhanced by using tcmalloc, then it would be nice to link your library with it, but the application might have other memory allocation patterns that are not a good fit with tcmalloc. So at the very least make it so your use of tcmalloc is made private (see the answer). If your library's performance doesn't really depend on tcmalloc, then I would not link with it at all, and leave it up to the application to decide what memory allocator to use. – G. Sliepen Nov 10 '20 at 12:33

1 Answers1

3

Is it ok to use tcmalloc in that shared library?

It depends on a few things:

  • Whether your shared library is linked with tcmalloc in such a way that it exposes malloc and operator new as external symbols. Normally, it does.
  • Whether the user of your library links to your library or loads it at run-time with dlopen and what dlopen options are used.

What happens if the user's executable itself is not linked with tcmalloc?

One of the two things can happen:

  1. malloc is already a resolved symbol in user's application/process. In this case your .so uses that malloc. That happens when the user loads your .so with dlopen.
  2. malloc is not resolved yet, so that user's application/process uses malloc from tcmalloc from your .so. That happens when the user links against your .so in linker command line and your .so comes before -lc.

It may be most robust for your .so to not link tcmalloc at all. The user of the application can then decide which malloc implementation to use either by linking against tcmalloc or other allocator, or trying different allocators by pre-loading them at run-time using LD_PRELOAD.

You may like to learn how Unix linkers work to answer such questions yourself in the future.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
  • @Jérémy https://github.com/google/tcmalloc/blob/master/docs/compatibility.md _Do not rely on dynamic loading/unloading. TCMalloc does not support dynamic loading and unloading._ – Maxim Egorushkin Nov 10 '20 at 18:11