Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Malloc Internals and You

March 2, 2017
DJ Delorie
Related topics:
Developer Tools

Share:

    Introduction

    In my last blog, I mentioned I was asked to look at a malloc performance issue, but discussed the methods for measuring performance. In this blog, I'll talk about the malloc issue itself, and some measures I took to address it. I'll also talk a bit about how malloc's internals work, and how that affects your performance.

    First off, a bit of terminology - throughout this blog, these terms are defined to mean as follows...

    memory
    a region of the process's virtual address space, independent of whether it's in RAM or swapped out to disk.
    allocated
    used to mean a chunk or region of memory is set aside for the application to use.
    chunk
    A region of contiguous memory which may be returned by malloc() or has been passed to free(), plus overhead.
    heap
    A large contiguous region of memory, in which chunks and other overhead (such as the arena structure) exist.
    arena
    A collection of one or more heaps, plus overhead needed to keep track of lists of chunks in various states, such as the fastbins and an unsorted chunk list.

    In an old-school single-threaded application, when you malloc memory, a chunk is chosen (or created) from the heap (back then, there was only one), and returned to the application. In a modern multi-threaded application[*], sometimes two threads want to malloc memory at the same time. If you're lucky, they don't step on each others' toes, but... you're not that lucky. So, malloc uses a lock to make the threads "take turns" accessing malloc's internal structures. Still, taking turns means one thread is doing nothing for a while, which can't be good for performance, and these locks themselves can take a significant amount of time as they often need to synchronize access across CPU caches and sometimes across physical CPUs.

    [*] Even if your application never starts a second thread, it's handled like a multi-threaded application, just in case.

    In The GNU C Library's (glibc's) malloc, this is partly addressed by allowing applications to have more than one arena from which memory can be allocated. Each arena "owns" one or more heaps, or regions of memory space, from which memory is allocated. If a thread needs to malloc memory and another thread has locked the arena it wants to use, it may switch to a different arena, or create a new one. A single-threaded application will only need one arena, since there won't be any thread contention. The number of arenas is limited to eight times the number of processor cores (for 64-bit systems, else for 32-bit, it's 2x), so if you have a 64-bit quad-core machine, you'll get up to 32 arenas. You can change this limit by setting the MALLOC_ARENA_MAX environment variable to the number of arenas you want.

    For further reading about glibc's malloc internals, visit the glibc Wiki's "Malloc Internals" page at https://sourceware.org/glibc/wiki/MallocInternals

    Tunings

    So what does this mean to your application? If you know how malloc works, you might be able to tweak your application, or tune glibc's implementation, to improve performance. Here are some things to try:

    • If you know how many threads will be making allocations, you can set MALLOC_ARENA_MAX (or, you can call mallopt() in your allocation) to set the number of arenas that make sense for your threads.
    • If your application mixes very large and small allocations, try setting the M_MMAP_THRESHOLD environment variable to just under the "very large" size. Allocations larger than this size go directly to mmap(), which means when you free() it, it won't end up in the middle of your heap and prevent coalescing of smaller chunks later.
    • Further, if you do lots of small allocations, changes to the MALLOC_TRIM_THRESHOLD environment variable might help keep your heap from being too fragmented.
    • The more consistent your allocation sizes are, the easier it is for malloc to find chunks to give you. However, malloc rounds up sizes internally to the size of two pointers (i.e. 8 bytes for 32-bit, or 16 bytes for 64-bit). If you separate "consistent" allocations in one thread and "varying" ones in another, they're more likely to be kept separate in separate arenas, which might help.
    • Likewise, if your allocations tend to be just a bit bigger than the internal alignment, you're going to be wasting a lot of address space. I.e. don't make a lot of 17-byte requests, because you'll waste 15 bytes per, or nearly half your memory! If you need a lot of these types of blocks, and they need not be as aligned as malloc guarantees, consider layering an app-specific cache of blocks over malloc, so you can pack them tightly and return them quickly.

    For further information about malloc tuning, see the malloc and mallopt man pages ("man malloc" and "man mallopt") and the glibc tunables documentation (a copy is installed with glibc, or see https://www.gnu.org/software/libc/manual/html_node/Tunables.html).

    Thread-Local Cache

    Still, every thread must lock an arena just in case some other thread comes along and tries to use it also. This lock turned out to be quite expensive compared to the cost of doing the allocation itself. Can we allocate memory without locking? There are two solutions, one which involves "lockless" programming using atomic operations (which are used for part of fastbins, an intermediate chunk cache inside malloc), and one which involves some thread-local storage (TLS) which can be accessed without needing a lock. I chose to use TLS to add a per-thread cache (tcache) of memory which could be quickly accessed, as TLS access does not require any locks or atomics. Memory returned to the heap via free() might be quickly stored in the tcache, so it can be quickly returned by a call to malloc(), without the cost of a lock. The cache might also be pre-filled once a lock is taken for other reasons.

    The tcache will be tunable via the following tunables (part of glibc 2.25's new tunables infrastructure, and subject to change):

    glibc.malloc.tcache_max
    the maximum size chunk that may be stored in a tcache (in bytes)
    glibc.malloc.tcache_count
    the maximum number of chunks of each size that may be stored in a tcache. Remember that chunk sizes are rounded up; "each size" refers to the rounded size, not the unrounded value you pass to malloc().
    glibc.malloc.tcache_unsorted_limit
    how many entries in the unsorted list are checked while trying to pre-fill the tcache.

    Thus, the maximum number of chunks that can be stored in a tcache is (for example, on a 64-bit machine) tcache_max * tcache_count / 16 (the 16 accounts for rounding).

    If you have many threads that do lots of mallocs and frees of a small number of block sizes, watch for a future update about my tcache work. It's currently being reviewed for inclusion in the upstream glibc. If you're impatient, you can checkout the dj/malloc-tcache branch from glibc's upstream git repo and play with it yourself :-)

    Last updated: March 5, 2017

    Recent Posts

    • How to use pipelines for AI/ML automation at the edge

    • What's new in network observability 1.8

    • LLM Compressor: Optimize LLMs for low-latency deployments

    • How to set up NVIDIA NIM on Red Hat OpenShift AI

    • Leveraging Ansible Event-Driven Automation for Automatic CPU Scaling in OpenShift Virtualization

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue

    OSZAR »