Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> 電腦軟體教學 >> 伺服器技術 >> 關於伺服器

LNMP 編譯整合 Google TCMalloc:優化 MySQL/Nginx 高併發記憶體管理

編輯:關於伺服器

什麼是 TCMalloc?

Thread-Caching Malloc (google-perftools) 專為多線程 C++ 應用優化的記憶體分配器,比 glibc 2.3+ malloc 更快、碎片更少、高併發下記憶體曲線更平滑。適用於 MySQL、Nginx 等高併發服務。

核心限制:靜態編譯必須在安裝前整合

LNMP 腳本預設對 MySQL、Nginx 採用靜態編譯,執行期無法動態載入 libtcmalloc.so。必須修改安裝腳本,在 ./configure 參數中加入編譯選項,並在安裝 LNMP 前先編譯安裝 TCMalloc。

完整操作流程

1. 安裝 TCMalloc 庫

wget https://github.com/gperftools/gperftools/releases/download/gperftools-2.10/gperftools-2.10.tar.gz
tar xzf gperftools-2.10.tar.gz
cd gperftools-2.10
./configure
make && make install
echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
ldconfig

2. 下載並修改 LNMP 安裝腳本

wget https://soft.vpser.net/lnmp/lnmp1.9.tar.gz  # 以最新版為例
tar xzf lnmp1.9.tar.gz
cd lnmp1.9
vi centos.sh  # 或對應發行版腳本

3. 修改 MySQL 編譯參數(約 306、308 行)

# 尾部追加
--with-mysqld-ldflags=-ltcmalloc

4. 修改 Nginx 編譯參數(約 460 行)

# 尾部追加
--with-google_perftools_module

5. 執行安裝

./centos.sh

6. Nginx 運行時啟用 TCMalloc 分析

mkdir -p /tmp/tcmalloc
chown -R www:www /tmp/tcmalloc
vi /usr/local/nginx/conf/nginx.conf
# 在 worker_rlimit_nofile 附近加入
google_perftools_profiles /tmp/tcmalloc/;
# 重載
nginx -s reload

7. 驗證生效

lsof -n | grep tcmalloc
# 應見 nginx 與 mysqld 進程持有 libtcmalloc.so

效能預期

  • MySQL 高併發下記憶體增長曲線平滑,減少 OOM 風險
  • Nginx 請求處理延遲抖動降低
  • 整體記憶體碎片率下降 30%+
Copyright © Windows教程網 All Rights Reserved