Convert Figma logo to code with AI

google logotcmalloc

No description available

4,311
464
4,311
55

Top Related Projects

10,348

mimalloc is a compact general purpose allocator with excellent performance.

Message passing based allocator

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.

Quick Overview

TCMalloc (Thread-Caching Malloc) is a fast memory allocator developed by Google. It's designed to be a drop-in replacement for the system malloc, providing efficient memory allocation and deallocation, especially in multi-threaded applications. TCMalloc aims to reduce lock contention and improve overall performance in memory-intensive programs.

Pros

  • Highly efficient for multi-threaded applications
  • Reduces lock contention and improves scalability
  • Provides detailed memory profiling and debugging tools
  • Can be used as a drop-in replacement for standard malloc

Cons

  • May not provide significant benefits for single-threaded or small-scale applications
  • Can increase memory usage due to per-thread caches
  • Requires careful configuration for optimal performance in specific use cases
  • Limited platform support compared to standard malloc implementations

Code Examples

  1. Basic usage (replacing standard malloc):
#include <tcmalloc/tcmalloc.h>

void* ptr = tc_malloc(1024);
// Use the allocated memory
tc_free(ptr);
  1. Using sized deallocation:
#include <tcmalloc/tcmalloc.h>

void* ptr = tc_malloc(1024);
// Use the allocated memory
tc_sized_free(ptr, 1024);
  1. Checking current memory usage:
#include <tcmalloc/tcmalloc.h>

size_t bytes_in_use = tc_malloc_size_class_stats(NULL, NULL, 0);
printf("Current memory usage: %zu bytes\n", bytes_in_use);

Getting Started

To use TCMalloc in your project:

  1. Install TCMalloc:

    git clone https://github.com/google/tcmalloc.git
    cd tcmalloc
    bazel build :tcmalloc
    
  2. Link your application with TCMalloc:

    g++ -o myapp myapp.cpp -ltcmalloc
    
  3. Alternatively, use LD_PRELOAD to use TCMalloc without relinking:

    LD_PRELOAD=/path/to/libtcmalloc.so ./myapp
    

Remember to include the appropriate headers in your code and link against the TCMalloc library when compiling your application.

Competitor Comparisons

10,348

mimalloc is a compact general purpose allocator with excellent performance.

Pros of mimalloc

  • Designed for better performance in multi-threaded applications
  • Simpler API and easier integration into existing projects
  • More frequent updates and active development

Cons of mimalloc

  • Less battle-tested in large-scale production environments
  • Smaller community and ecosystem compared to TCMalloc
  • May not perform as well for specific use cases optimized by TCMalloc

Code comparison

mimalloc:

#include <mimalloc.h>

void* p = mi_malloc(sizeof(int));
mi_free(p);

TCMalloc:

#include <gperftools/tcmalloc.h>

void* p = tc_malloc(sizeof(int));
tc_free(p);

Both libraries aim to provide efficient memory allocation, but mimalloc focuses on simplicity and ease of use, while TCMalloc offers more advanced features and customization options. mimalloc's API is more straightforward, making it easier to integrate into existing projects. However, TCMalloc has a longer history and may be more suitable for specific large-scale applications that benefit from its optimizations.

The choice between the two depends on the specific requirements of your project, such as performance needs, ease of integration, and the target environment.

Pros of jemalloc

  • Better performance for multi-threaded applications
  • More consistent and predictable memory usage patterns
  • Extensive profiling and debugging tools built-in

Cons of jemalloc

  • Slightly higher memory overhead in some cases
  • Less integrated with Google's ecosystem and tools
  • May require more manual tuning for optimal performance

Code Comparison

tcmalloc:

void* malloc(size_t size) {
  void* result = do_malloc(size);
  MallocHook::InvokeNewHook(result, size);
  return result;
}

jemalloc:

void *
je_malloc(size_t size)
{
    void *ret;
    if (malloc_init())
        return (NULL);
    ret = imalloc(size);
    return (ret);
}

Both implementations provide similar core functionality, but jemalloc's approach is more straightforward, while tcmalloc includes additional hooks for customization and monitoring.

Message passing based allocator

Pros of snmalloc

  • Designed for better scalability in multi-threaded environments
  • Implements a novel "message passing" allocation strategy for improved performance
  • Offers better security features, including randomization and guard pages

Cons of snmalloc

  • Less mature and less widely adopted compared to tcmalloc
  • May have higher memory overhead in certain scenarios
  • Limited documentation and community support

Code Comparison

snmalloc:

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

tcmalloc:

void* alloc = tc_malloc(size);
tc_free(alloc);

Both allocators provide similar APIs, but snmalloc uses a thread-local allocator object, while tcmalloc uses global functions. snmalloc's approach can lead to better performance in multi-threaded scenarios.

snmalloc focuses on scalability and security, making it potentially better suited for large-scale, multi-threaded applications with high-security requirements. However, tcmalloc has a longer track record and wider adoption, which may make it a safer choice for many projects. The choice between the two depends on specific project needs, performance requirements, and the importance of cutting-edge features versus established reliability.

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

Pros of rpmalloc

  • Lightweight and portable, with minimal dependencies
  • Designed for cross-platform use, including game consoles
  • Faster allocation and deallocation for small objects

Cons of rpmalloc

  • Less mature and less widely adopted than TCMalloc
  • May not perform as well for large-scale, multi-threaded applications
  • Limited documentation and community support compared to TCMalloc

Code Comparison

TCMalloc:

#include <tcmalloc/tcmalloc.h>

void* ptr = tc_malloc(size);
tc_free(ptr);

rpmalloc:

#include <rpmalloc.h>

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

Both libraries aim to provide efficient memory allocation, but they have different focuses. TCMalloc is designed for large-scale applications and offers advanced features like heap profiling. rpmalloc prioritizes simplicity, portability, and performance for small allocations, making it suitable for games and embedded systems. The choice between them depends on the specific requirements of your project, such as target platforms, application size, and performance needs.

1,739

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

Pros of Mesh

  • Focuses on reducing memory fragmentation through mesh-based allocation
  • Designed to be a drop-in replacement for malloc, making integration easier
  • Potentially better performance for applications with high memory churn

Cons of Mesh

  • Less mature and battle-tested compared to TCMalloc
  • May have higher overhead for small allocations
  • Limited platform support compared to TCMalloc's wide adoption

Code Comparison

TCMalloc:

void* TCMalloc_Malloc(size_t size) {
  void* result = do_malloc(size);
  MallocHook::InvokeNewHook(result, size);
  return result;
}

Mesh:

void* mesh_malloc(size_t sz) {
  void* ptr = _mesh_malloc(sz);
  if (unlikely(ptr == nullptr))
    return nullptr;
  return ptr;
}

Both implementations provide similar interfaces for memory allocation, but Mesh's implementation is more straightforward, reflecting its focus on simplicity and ease of integration. TCMalloc's implementation includes additional hooks for monitoring and customization, showcasing its more feature-rich nature.

TCMalloc is widely used in large-scale production environments and offers extensive performance optimizations. Mesh, on the other hand, provides a novel approach to memory management that may be beneficial for specific use cases, particularly those involving large allocations or high fragmentation scenarios.

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

TCMalloc

This repository contains the TCMalloc C++ code.

TCMalloc is Google's customized implementation of C's malloc() and C++'s operator new used for memory allocation within our C and C++ code. TCMalloc is a fast, multi-threaded malloc implementation.

Building TCMalloc

Bazel is the official build system for TCMalloc.

The TCMalloc Platforms Guide contains information on platform support for TCMalloc.

Documentation

All users of TCMalloc should consult the following documentation resources:

  • The TCMalloc Quickstart covers downloading, installing, building, and testing TCMalloc, including incorporating within your codebase.
  • The TCMalloc Overview covers the basic architecture of TCMalloc, and how that may affect configuration choices.
  • The TCMalloc Reference covers the C and C++ TCMalloc API endpoints.

More advanced usages of TCMalloc may find the following documentation useful:

  • The TCMalloc Tuning Guide covers the configuration choices in more depth, and also illustrates other ways to customize TCMalloc. This also covers important operating system-level properties for improving TCMalloc performance.
  • The TCMalloc Design Doc covers how TCMalloc works underneath the hood, and why certain design choices were made. Most developers will not need this level of implementation detail.
  • The TCMalloc Compatibility Guide which documents our expectations for how our APIs are used.

License

The TCMalloc library is licensed under the terms of the Apache license. See LICENSE for more information.

Disclaimer: This is not an officially supported Google product.