diff --git a/2-memory/benchmark/README.md b/2-memory/benchmark/README.md index 0080419919fd0b97987206f3eb58d7a4109fd85b..1aac01fd7a0e0c05597be572b0506389795d6b69 100644 --- a/2-memory/benchmark/README.md +++ b/2-memory/benchmark/README.md @@ -1,33 +1,41 @@ -### 编译tcmalloc库 +### 1 在栈中分配内存速度更快 +heap_stack.java验证在栈中分配内存比堆中更快 +#### 1.1 编译 +javac heap_stack.java +#### 1.2 运行 +java heap_stack +### 2 编译tcmalloc库 +使用TCMalloc库前,需要下载编译它 #### 预安装库 autoconf、automake、libtool CentOS下安装:yum install autoconf automake libtool Ubuntu下安装:apt-get install autoconf、automake、libtool -#### 下载gperftool +#### 2.1 下载gperftool https://github.com/gperftools/gperftools.git -#### 生成configure文件 +#### 2.2 生成configure文件 ./autogen.sh -#### 生成Makefile文件 +#### 2.3 生成Makefile文件 ./configure *配置选项通过--help查询* -#### 编译 +#### 2.4 编译 make -#### 安装 +#### 2.5 安装 make install -### benchmark性能测试 - -#### 编译benchmark文件 +### 3 benchmark性能测试 +对比测试tcmalloc与ptmalloc2的性能 +#### 3.1 编译benchmark文件 g++ benchmark.cpp -o benchmark -lpthread -#### benchmark命令行选项 +#### 3.2 benchmark命令行选项 1. -s: 分配内存块大小,单位字节 2. -t: 线程数 3. -n: 每个线程循环分配内存的次数 -#### 基于ptmalloc2测试 +#### 3.3 基于ptmalloc2测试 + ##### 单线程分配256KB export LD_PRELOAD=""; ./benchmark -s 262144 -t 1 ##### 2线程分配256KB @@ -39,7 +47,7 @@ export LD_PRELOAD=""; ./benchmark -s 262145 -t 1 ##### 10线程分配256KB+1字节 export LD_PRELOAD=""; ./benchmark -s 262145 -t 10 -#### 基于TcMalloc测试 +#### 3.4 基于TcMalloc测试 *根据configure生成的libtcmalloc.so文件的位置填写LD_PRELOAD* ##### 单线程分配256KB export LD_PRELOAD="/lib64/libtcmalloc.so"; ./benchmark -s 262144 -t 1 diff --git a/2-memory/benchmark/benchmark.cpp b/2-memory/benchmark/benchmark.cpp index 7fa3908b0f26297195e5ca9a0ed72cb13b39be79..a5474b3296727fb36b56298c7a777bfde1113322 100644 --- a/2-memory/benchmark/benchmark.cpp +++ b/2-memory/benchmark/benchmark.cpp @@ -1,11 +1,9 @@ #include "stdio.h" #include #include -#include #include #include -using namespace std; int TESTN = 128*1024*1024; int size = 64; @@ -61,7 +59,7 @@ int main(int argc, char** argv) { int ret=pthread_create(&id[i],NULL,loopalloc,&timecost[i]); if(ret!=0){ - cout<<"Create pthread error!\n"; + printf("Create pthread error!\n"); exit (1); } }