Skip to content

Instantly share code, notes, and snippets.

@corporatepiyush
Last active May 29, 2024 12:49
Show Gist options
  • Save corporatepiyush/318c0c6970caee8af36360a8f5e2df4c to your computer and use it in GitHub Desktop.
Save corporatepiyush/318c0c6970caee8af36360a8f5e2df4c to your computer and use it in GitHub Desktop.
Memory optimisations using jemalloc and tcmalloc on Linux
#!/bin/sh
# touch /etc/profile.d/malloc_config.sh
# Get total RAM size in MB
total_ram=$(free -m | awk '/^Mem:/{print $2}')
if [ "$total_ram" -lt 8192 ]; then
# Low RAM settings
export MALLOC_ARENA_MAX=2
export MALLOC_MMAP_THRESHOLD_=65536 # 64 KB
export MALLOC_TRIM_THRESHOLD_=131072 # 128 KB
export MALLOC_TOP_PAD_=32768 # 32 KB
elif [ "$total_ram" -lt 32768 ]; then
# Moderate RAM settings
export MALLOC_ARENA_MAX=4
export MALLOC_MMAP_THRESHOLD_=131072 # 128 KB
export MALLOC_TRIM_THRESHOLD_=262144 # 256 KB
export MALLOC_TOP_PAD_=65536 # 64 KB
elif [ "$total_ram" -lt 65536 ]; then
# High RAM settings
export MALLOC_ARENA_MAX=6
export MALLOC_MMAP_THRESHOLD_=196608 # 192 KB
export MALLOC_TRIM_THRESHOLD_=393216 # 384 KB
export MALLOC_TOP_PAD_=98304 # 96 KB
else
# Highest RAM settings
export MALLOC_ARENA_MAX=8
export MALLOC_MMAP_THRESHOLD_=262144 # 256 KB
export MALLOC_TRIM_THRESHOLD_=524288 # 512 KB
export MALLOC_TOP_PAD_=131072 # 128 KB
fi
export MALLOC_MMAP_MAX_=65536
export MALLOC_CHECK_=0
export MALLOC_PERTURB_=0
# sudo chmod +x /etc/profile.d/malloc_config.sh
#!/bin/bash
# Update package lists
sudo yum update -y
# Install dependencies
sudo yum groupinstall -y "Development Tools"
sudo yum install -y autoconf automake libtool
# Download jemalloc source code
JEMALLOC_VERSION=5.3.0
wget https://github.com/jemalloc/jemalloc/releases/download/${JEMALLOC_VERSION}/jemalloc-${JEMALLOC_VERSION}.tar.bz2
# Extract the downloaded tarball
tar -xvf jemalloc-${JEMALLOC_VERSION}.tar.bz2
# Change to the extracted directory
cd jemalloc-${JEMALLOC_VERSION}
# Compile and install jemalloc
./autogen.sh
make
sudo make install
grep -z 'LD_PRELOAD' /proc/<pid>/environ | tr '\0' '\n'
# if jemalloc is listed among the loaded libraries for a process.
lsof -p <pid> | grep jemalloc
# to list the shared libraries used by an executable
ldd /path/to/your/application | grep jemalloc
# Create a systemd override directory for MySQL
sudo mkdir -p /etc/systemd/system/mysql.service.d
# Create a systemd override file for MySQL
echo -e "[Service]\nEnvironment=\"LD_PRELOAD=/usr/local/lib/libjemalloc.so\"\nEnvironment=\"MALLOC_CONF='dirty_decay_ms:1000,narenas:2,background_thread:true'\"" | sudo tee /etc/systemd/system/mysqld.service.d/override.conf
# Reload systemd and restart MySQL
sudo systemctl daemon-reload
sudo systemctl restart mysql
# Set jemalloc as the default allocator and MALLOC_ARENA_MAX=2 at the OS level
echo 'export LD_PRELOAD=/usr/local/lib/libjemalloc.so' | sudo tee -a /etc/profile.d/jemalloc.sh
echo "export MALLOC_CONF='dirty_decay_ms:1000,narenas:2,background_thread:true'" | sudo tee -a /etc/profile.d/jemalloc.sh
# Make the script executable
sudo chmod +x /etc/profile.d/jemalloc.sh
# Apply the environment variables immediately
source /etc/profile.d/jemalloc.sh
# Create a systemd override directory for PostgreSQL
sudo mkdir -p /etc/systemd/system/postgresql.service.d
# Create a systemd override file for PostgreSQL
echo -e "[Service]\nEnvironment=\"LD_PRELOAD=/usr/local/lib/libjemalloc.so\"\nEnvironment=\"MALLOC_CONF='dirty_decay_ms:1000,narenas:2,background_thread:true'\"" | sudo tee /etc/systemd/system/postgresql.service.d/override.conf
# Reload systemd and restart PostgreSQL
sudo systemctl daemon-reload
sudo systemctl restart postgresql
# Add LD_PRELOAD to .bashrc
echo 'export LD_PRELOAD=/usr/local/lib/libjemalloc.so' >> ~/.bashrc
echo "export MALLOC_CONF='dirty_decay_ms:1000,narenas:2,background_thread:true'" >> ~/.bashrc
# Reload .bashrc to apply changes
source ~/.bashrc
# Add LD_PRELOAD to .bashrc
echo 'export LD_PRELOAD=/usr/local/lib/libjemalloc.so' >> ~/.zshrc
echo "export MALLOC_CONF='dirty_decay_ms:1000,narenas:2,background_thread:true'" >> ~/.zshrc
# Reload .zshrc to apply changes
source ~/.zshrc
#!/bin/bash
# Update package lists
sudo apt-get update
sudo apt-get upgrade
# Install dependencies
sudo apt-get install -y autoconf automake libtool make gcc
# Download jemalloc source code
JEMALLOC_VERSION=5.3.0
wget https://github.com/jemalloc/jemalloc/releases/download/${JEMALLOC_VERSION}/jemalloc-${JEMALLOC_VERSION}.tar.bz2
# Extract the downloaded tarball
tar -xvf jemalloc-${JEMALLOC_VERSION}.tar.bz2
# Change to the extracted directory
cd jemalloc-${JEMALLOC_VERSION}
# Compile and install jemalloc
./autogen.sh
make
sudo make install
#!/bin/bash
# Install TCMalloc
sudo apt-get update
sudo apt-get install -y google-perftools
# Get total RAM in KB
total_ram_kb=$(grep MemTotal /proc/meminfo | awk '{print $2}')
# Convert total RAM to GB
total_ram_gb=$((total_ram_kb / 1024 / 1024))
# Set default values for different RAM configurations
if [ $total_ram_gb -le 8 ]; then
large_alloc_threshold=33554432 # 32 MB
max_total_thread_cache_bytes=67108864 # 64 MB
aggressive_decommit=true
transfer_batch_size=32
elif [ $total_ram_gb -le 16 ]; then
large_alloc_threshold=67108864 # 64 MB
max_total_thread_cache_bytes=268435456 # 256 MB
aggressive_decommit=true
transfer_batch_size=64
elif [ $total_ram_gb -le 32 ]; then
large_alloc_threshold=100663296 # 96 MB
max_total_thread_cache_bytes=536870912 # 512 MB
aggressive_decommit=true
transfer_batch_size=64
else
large_alloc_threshold=134217728 # 128 MB
max_total_thread_cache_bytes=1073741824 # 1 GB
aggressive_decommit=true
transfer_batch_size=128
fi
# Export environment variables
echo "Setting TCMalloc parameters based on ${total_ram_gb}GB RAM..."
export LD_PRELOAD="/usr/lib/libtcmalloc.so"
export TCMALLOC_LARGE_ALLOC_REPORT_THRESHOLD=$large_alloc_threshold
export TCMALLOC_MAX_TOTAL_THREAD_CACHE_BYTES=$max_total_thread_cache_bytes
export TCMALLOC_AGGRESSIVE_DECOMMIT=$aggressive_decommit
export TCMALLOC_TRANSFER_BATCH_SIZE=$transfer_batch_size
# Add to /etc/profile for global settings
echo "export LD_PRELOAD='/usr/lib/libtcmalloc.so'" | sudo tee -a /etc/profile
echo "export TCMALLOC_LARGE_ALLOC_REPORT_THRESHOLD=$large_alloc_threshold" | sudo tee -a /etc/profile
echo "export TCMALLOC_MAX_TOTAL_THREAD_CACHE_BYTES=$max_total_thread_cache_bytes" | sudo tee -a /etc/profile
echo "export TCMALLOC_AGGRESSIVE_DECOMMIT=$aggressive_decommit" | sudo tee -a /etc/profile
echo "export TCMALLOC_TRANSFER_BATCH_SIZE=$transfer_batch_size" | sudo tee -a /etc/profile
@mokshchadha
Copy link

mokshchadha commented May 24, 2024

very helpful script

@mokshchadha
Copy link

To make sure you are using the correct path for your jemalloc lib file one can use

find /usr -name "libjemalloc.so*"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment