Convert Figma logo to code with AI

emeryberger logoHoard

The Hoard Memory Allocator: A Fast, Scalable, and Memory-efficient Malloc for Linux, Windows, and Mac.

1,088
127
1,088
11

Top Related Projects

10,348

mimalloc is a compact general purpose allocator with excellent performance.

Main gperftools repository

Public domain cross platform lock free thread caching 16-byte aligned memory allocator implemented in C

1,739

A memory allocator that automatically reduces the memory footprint of C/C++ applications.

Message passing based allocator

Quick Overview

Hoard is a fast, scalable memory allocator designed for multi-threaded applications. It aims to improve performance and reduce memory fragmentation in programs that make heavy use of dynamic memory allocation, especially in multi-core environments.

Pros

  • Highly scalable, particularly effective for multi-threaded applications
  • Reduces memory fragmentation, leading to better memory utilization
  • Improves overall application performance, especially in memory-intensive scenarios
  • Compatible with existing C and C++ programs without requiring code changes

Cons

  • May not provide significant benefits for single-threaded or low-memory-usage applications
  • Can increase memory usage in some cases due to its memory management strategy
  • Requires linking against the Hoard library, which may complicate build processes
  • Limited documentation and examples available compared to standard allocators

Code Examples

  1. Basic usage in C:
#include <hoard.h>
#include <stdio.h>

int main() {
    void* ptr = malloc(1024);
    // Use the allocated memory
    free(ptr);
    return 0;
}
  1. C++ example with new and delete:
#include <hoard.h>
#include <iostream>

class MyClass {
    // Class definition
};

int main() {
    MyClass* obj = new MyClass();
    // Use the object
    delete obj;
    return 0;
}
  1. Custom allocation size example:
#include <hoard.h>
#include <stdio.h>

int main() {
    void* largeBlock = malloc(1024 * 1024); // Allocate 1MB
    // Use the large block
    free(largeBlock);
    return 0;
}

Getting Started

To use Hoard in your project:

  1. Download and build Hoard from the GitHub repository.
  2. Link your program with the Hoard library:
    • For GCC: g++ -o myprogram myprogram.cpp -L/path/to/hoard -lhoard
    • For Visual Studio: Add Hoard.lib to your project's linker input.
  3. On Linux, you may need to preload the Hoard library:
    LD_PRELOAD=/path/to/libhoard.so ./myprogram
    
  4. Your program will now use Hoard as its memory allocator without any code changes.

Competitor Comparisons

Pros of jemalloc

  • Better performance in multi-threaded environments
  • More advanced memory fragmentation reduction techniques
  • Wider adoption and community support

Cons of jemalloc

  • More complex configuration and tuning required
  • Potentially higher memory overhead in some scenarios
  • Less portable across different operating systems

Code Comparison

jemalloc:

#include <jemalloc/jemalloc.h>

void *ptr = malloc(size);
je_free(ptr);

Hoard:

#include <hoard.h>

void *ptr = xxmalloc(size);
xxfree(ptr);

Key Differences

  • jemalloc offers more fine-grained control over memory allocation strategies
  • Hoard focuses on simplicity and ease of use
  • jemalloc provides extensive statistics and profiling tools
  • Hoard emphasizes scalability in multi-threaded applications

Use Cases

  • jemalloc: Large-scale applications with complex memory requirements
  • Hoard: Applications prioritizing simplicity and cross-platform compatibility

Community and Support

  • jemalloc: Larger community, more frequent updates
  • Hoard: Smaller community, less frequent updates but still actively maintained
10,348

mimalloc is a compact general purpose allocator with excellent performance.

Pros of mimalloc

  • Designed for high performance and scalability, often outperforming Hoard in benchmarks
  • Offers extensive customization options and fine-grained control over memory allocation
  • Provides better support for modern CPU architectures and memory models

Cons of mimalloc

  • May have a steeper learning curve due to its more complex API and configuration options
  • Less mature project compared to Hoard, potentially leading to fewer community resources

Code Comparison

mimalloc:

#include <mimalloc.h>

void* ptr = mi_malloc(size);
mi_free(ptr);

Hoard:

#include <hoard.h>

void* ptr = xxmalloc(size);
xxfree(ptr);

Both libraries aim to provide efficient memory allocation, but mimalloc offers more advanced features and optimizations. Hoard focuses on simplicity and ease of use, while mimalloc prioritizes performance and customization. The choice between them depends on specific project requirements and performance needs.

Main gperftools repository

Pros of gperftools

  • More comprehensive toolset, including CPU and heap profilers
  • Wider platform support, including non-x86 architectures
  • Active development and maintenance

Cons of gperftools

  • Higher complexity and learning curve
  • Potentially larger memory footprint
  • May require more setup and configuration

Code Comparison

gperftools:

#include <gperftools/tcmalloc.h>

void* custom_alloc(size_t size) {
  return tc_malloc(size);
}

Hoard:

#include "hoard.h"

void* custom_alloc(size_t size) {
  return xxmalloc(size);
}

Key Differences

  • gperftools offers a more extensive suite of performance tools, while Hoard focuses primarily on memory allocation
  • Hoard is designed for simplicity and ease of use, whereas gperftools provides more advanced features
  • gperftools includes profiling capabilities, which are not present in Hoard
  • Hoard may have better performance in certain multithreaded scenarios due to its scalable memory allocator design

Both libraries aim to improve memory management and overall application performance, but they take different approaches. The choice between them depends on specific project requirements, platform constraints, and the desired balance between simplicity and feature richness.

Public domain cross platform lock free thread caching 16-byte aligned memory allocator implemented in C

Pros of rpmalloc

  • Designed for high performance and low memory overhead
  • Supports cross-platform development, including game consoles
  • Offers thread-local caching for improved speed in multi-threaded applications

Cons of rpmalloc

  • Less mature and less widely adopted compared to Hoard
  • May require more manual configuration for optimal performance
  • Limited documentation and community support

Code Comparison

rpmalloc:

void* ptr = rpmalloc(size);
rpfree(ptr);

Hoard:

#include <hoard.h>
void* ptr = xxmalloc(size);
xxfree(ptr);

Key Differences

  • rpmalloc focuses on performance and low memory overhead, while Hoard emphasizes scalability and reduced fragmentation
  • rpmalloc uses a thread-local caching mechanism, whereas Hoard employs a global heap structure
  • rpmalloc is more lightweight and may be easier to integrate into existing projects, while Hoard offers a more comprehensive memory management solution

Use Cases

  • rpmalloc: Ideal for performance-critical applications, especially in game development and embedded systems
  • Hoard: Well-suited for large-scale, multi-threaded applications where scalability and reduced fragmentation are crucial
1,739

A memory allocator that automatically reduces the memory footprint of C/C++ applications.

Pros of Mesh

  • Offers better memory fragmentation handling through meshing
  • Provides improved security features like randomized allocation
  • Supports large-scale multithreaded applications more efficiently

Cons of Mesh

  • May have higher overhead for small allocations
  • Potentially more complex implementation and maintenance
  • Could be less suitable for systems with limited memory resources

Code Comparison

Hoard:

void * xxmalloc(size_t sz) {
  if (sz == 0) {
    return NULL;
  }
  void * ptr = getHeader(sz)->malloc(sz);
  if (ptr == NULL) {
    return NULL;
  }
  return ptr;
}

Mesh:

void *MeshHeap::malloc(size_t sz) {
  if (unlikely(sz == 0)) {
    sz = 1;
  }
  const auto sizeClass = SizeMap::SizeClass(sz);
  void *ptr = _current[sizeClass].malloc(sizeClass);
  if (unlikely(ptr == nullptr)) {
    ptr = slowPathMalloc(sz);
  }
  return ptr;
}

Both allocators implement custom malloc functions, but Mesh's implementation includes size class handling and a slow path for complex allocations, potentially offering more flexibility and optimization opportunities.

Message passing based allocator

Pros of snmalloc

  • Designed for high scalability and performance in multi-threaded environments
  • Implements a novel "message passing" allocation strategy for improved efficiency
  • Offers better support for modern CPU architectures and memory models

Cons of snmalloc

  • May have higher memory overhead in certain scenarios compared to Hoard
  • Less mature and widely adopted than Hoard, potentially leading to fewer community resources

Code Comparison

snmalloc:

void* alloc = snmalloc::ThreadAlloc::get().alloc(size);
snmalloc::ThreadAlloc::get().dealloc(alloc);

Hoard:

void* alloc = hoard_malloc(size);
hoard_free(alloc);

Both allocators aim to provide efficient memory management for multi-threaded applications, but they employ different strategies. snmalloc focuses on a message-passing approach and modern CPU optimizations, while Hoard emphasizes scalability through its heap layering technique.

snmalloc may offer better performance in highly concurrent scenarios, especially on newer hardware. However, Hoard has a longer track record and may be more suitable for applications requiring broad compatibility.

The code comparison shows that both allocators provide similar interfaces for allocation and deallocation, making it relatively straightforward to switch between them in most cases.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

The Hoard Memory Allocator

by Emery Berger

The Hoard memory allocator is a fast, scalable, and memory-efficient memory allocator that works on a range of platforms, including Linux, Mac OS X, and Windows.

Hoard is a drop-in replacement for malloc that can dramatically improve application performance, especially for multithreaded programs running on multiprocessors and multicore CPUs. No source code changes necessary: just link it in or set one environment variable (see Building Hoard, below).

Downloads

Press

Users

Companies using Hoard in their products and servers include AOL, British Telecom, Blue Vector, Business Objects (formerly Crystal Decisions), Cisco, Credit Suisse, Entrust, InfoVista, Kamakura, Novell, Oktal SE, OpenText, OpenWave Systems (for their Typhoon and Twister servers), Pervasive Software, Plath GmbH, Quest Software, Reuters, Royal Bank of Canada, SAP, Sonus Networks, Tata Communications, and Verite Group.

Open source projects using Hoard include the Asterisk Open Source Telephony Project, Bayonne GNU telephony server, the Cilk parallel programming language, the GNU Common C++ system, the OpenFOAM computational fluid dynamics toolkit, and the SafeSquid web proxy.

Hoard is now a standard compiler option for the Standard Performance Evaluation Corporation's CPU2006 benchmark suite for the Intel and Open64 compilers.

Licensing

Hoard has now been released under the widely-used and permissive Apache license, version 2.0.

Why Hoard?

There are a number of problems with existing memory allocators that make Hoard a better choice.

Contention

Multithreaded programs often do not scale because the heap is a bottleneck. When multiple threads simultaneously allocate or deallocate memory from the allocator, the allocator will serialize them. Programs making intensive use of the allocator actually slow down as the number of processors increases. Your program may be allocation-intensive without you realizing it, for instance, if your program makes many calls to the C++ Standard Template Library (STL). Hoard eliminates this bottleneck.

False Sharing

System-provided memory allocators can cause insidious problems for multithreaded code. They can lead to a phenomenon known as "false sharing": threads on different CPUs can end up with memory in the same cache line, or chunk of memory. Accessing these falsely-shared cache lines is hundreds of times slower than accessing unshared cache lines. Hoard is designed to prevent false sharing.

Blowup

Multithreaded programs can also lead the allocator to blowup memory consumption. This effect can multiply the amount of memory needed to run your application by the number of CPUs on your machine: four CPUs could mean that you need four times as much memory. Hoard is guaranteed (provably!) to bound memory consumption.

Installation

Homebrew (Mac OS X)

You can use Homebrew to install the current version of Hoard as follows:

brew tap emeryberger/hoard
brew install --HEAD emeryberger/hoard/libhoard

This not only installs the Hoard library, but also creates a hoard command you can use to run Hoard with anything at the command-line.

hoard myprogram-goes-here

Building Hoard from source (Mac OS X, Linux, and Windows WSL2)

On Linux, you may need to first install the appropriate version of libstdc++-dev (e.g., libstdc++-12-dev):

   sudo apt install libstdc++-dev

Now, to build Hoard from source, do the following:

    git clone https://github.com/emeryberger/Hoard
    cd src
    make

You can then use Hoard by linking it with your executable, or by setting the LD_PRELOAD environment variable, as in

    export LD_PRELOAD=/path/to/libhoard.so

or, in Mac OS X:

    export DYLD_INSERT_LIBRARIES=/path/to/libhoard.dylib

Building Hoard (Windows)

Change into the src directory and build the Windows version:

C:\hoard\src> nmake

To use Hoard, link your executable with source\uselibhoard.cpp and libhoard.lib. You must use the /MD flag.

Example:

C:\hoard\src> cl /Ox /MD yourapp.cpp source\uselibhoard.cpp libhoard.lib

To run yourapp.exe, you will need to have libhoard.dll in your path.

Benchmarks

The directory benchmarks/ contains a number of benchmarks used to evaluate and tune Hoard.

Technical Information

Hoard has changed quite a bit over the years, but for technical details of the first version of Hoard, read Hoard: A Scalable Memory Allocator for Multithreaded Applications, by Emery D. Berger, Kathryn S. McKinley, Robert D. Blumofe, and Paul R. Wilson. The Ninth International Conference on Architectural Support for Programming Languages and Operating Systems (ASPLOS-IX). Cambridge, MA, November 2000.