Skip to content

Commit 625488a

Browse files
committed
util: Work around (virtual) memory exhaustion on 32-bit w/ glibc
glibc-specific: On 32-bit systems set the number of arenas to 1. By default, since glibc 2.10, the C library will create up to two heap arenas per core. This is known to cause excessive virtual address space usage in our usage. Work around it by setting the maximum number of arenas to 1.
1 parent f34cdcb commit 625488a

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

configure.ac

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,14 @@ AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <malloc.h>]],
574574
[ AC_MSG_RESULT(no)]
575575
)
576576

577+
dnl Check for mallopt(M_ARENA_MAX) (to set glibc arenas)
578+
AC_MSG_CHECKING(for mallopt M_ARENA_MAX)
579+
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <malloc.h>]],
580+
[[ mallopt(M_ARENA_MAX, 1); ]])],
581+
[ AC_MSG_RESULT(yes); AC_DEFINE(HAVE_MALLOPT_ARENA_MAX, 1,[Define this symbol if you have mallopt with M_ARENA_MAX]) ],
582+
[ AC_MSG_RESULT(no)]
583+
)
584+
577585
AC_MSG_CHECKING([for visibility attribute])
578586
AC_LINK_IFELSE([AC_LANG_SOURCE([
579587
int foo_def( void ) __attribute__((visibility("default")));

src/util.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@
7272
#include <sys/prctl.h>
7373
#endif
7474

75+
#ifdef HAVE_MALLOPT_ARENA_MAX
76+
#include <malloc.h>
77+
#endif
78+
7579
#include <boost/algorithm/string/case_conv.hpp> // for to_lower()
7680
#include <boost/algorithm/string/join.hpp>
7781
#include <boost/algorithm/string/predicate.hpp> // for startswith() and endswith()
@@ -792,6 +796,16 @@ void RenameThread(const char* name)
792796

793797
void SetupEnvironment()
794798
{
799+
#ifdef HAVE_MALLOPT_ARENA_MAX
800+
// glibc-specific: On 32-bit systems set the number of arenas to 1.
801+
// By default, since glibc 2.10, the C library will create up to two heap
802+
// arenas per core. This is known to cause excessive virtual address space
803+
// usage in our usage. Work around it by setting the maximum number of
804+
// arenas to 1.
805+
if (sizeof(void*) == 4) {
806+
mallopt(M_ARENA_MAX, 1);
807+
}
808+
#endif
795809
// On most POSIX systems (e.g. Linux, but not BSD) the environment's locale
796810
// may be invalid, in which case the "C" locale is used as fallback.
797811
#if !defined(WIN32) && !defined(MAC_OSX) && !defined(__FreeBSD__) && !defined(__OpenBSD__)

0 commit comments

Comments
 (0)