编译过程:
0 cygwin安装各种工具
* autoconf
* autogen
* gawk
* grep
* sed
1、下载源码
2、生成构建信息sh -c "CC=cl ./autogen.sh"
3、make -j 12代码构建
3、make install 生成库文件
路径:E:\sourceCodeTest\jemalloc\lib
使用方法:
#include "jemalloc/jemalloc.h" // “配置属性”->“C/C++”->“常规”->“附加包含目录”
//#define USE_JEMALLOC // 重载malloc/free
//#define JEMALLOC_NO_DEMANGLE
#ifdef USE_JEMALLOC
#define malloc(size) je_malloc(size)
#define calloc(count,size) je_calloc(count,size)
#define realloc(ptr,size) je_realloc(ptr,size)
#define free(ptr) je_free(ptr)
#endif
/*
// 当前使用的是jemalloc静态库
#ifdef _DEBUG
#pragma comment(lib,"jemalloc-x86-Debug-static.lib")
#else
#pragma comment(lib,"jemalloc-x86-Release-static.lib")
#endif
*/
/*
// 重新定义C++的内存分配函数,由jemalloc来进行内存分配
void* operator new(size_t size) {
return je_malloc(size);
}
void* operator new(size_t size, const std::nothrow_t& nothrow_value) {
return je_malloc(size);
}
void* operator new[](size_t size) {
return je_malloc(size);
}
void* operator new[](size_t size, const std::nothrow_t& nothrow_value) {
return je_malloc(size);
}
void operator delete(void* p) {
je_free(p);
}
void operator delete(void* p, const std::nothrow_t& nothrow_value) {
je_free(p);
}
void operator delete[](void* p, const std::nothrow_t& nothrow_value) {
je_free(p);
}
*/
int jemallocTest(int iCount, int iSize)
{
int i = 0;
// malloc
auto start = std::chrono::steady_clock::now();
for (i = 0; i < iCount; i++)
{
void* pMem = malloc(iSize);
assert(pMem);
free(pMem);
}
auto end = std::chrono::steady_clock::now();
// new
auto start2 = std::chrono::steady_clock::now();
for (i = 0; i < iCount; i++)
{
void* ptr1 = new(char[iSize]);
delete(ptr1);
}
auto end2 = std::chrono::steady_clock::now();
// jemalloc
auto start1 = std::chrono::steady_clock::now();
for (i = 0; i < iCount; i++)
{
void* pMem = je_malloc(iSize);
assert(pMem);
je_free(pMem);
}
auto end1 = std::chrono::steady_clock::now();
printf("malloc span time = %lld, new span time = %lld, jemalloc span time = %lld\r\n",
std::chrono::duration_cast<std::chrono::microseconds>(end - start).count(),
std::chrono::duration_cast<std::chrono::microseconds>(end2 - start2).count(),
std::chrono::duration_cast<std::chrono::microseconds>(end1 - start1).count());
printf("press any key exit......\r\n");
return 0;
}
#include <thread>
int main(int argc, char** argv) {
// mutil threads
std::thread threads[4];
std::cout << "Spawning 4 threads...\n";
int count= 5000;
int size[4] = {64, 1024, 4096, 4096*3};
for (int i = 0; i < 4; i++) {
threads[i] = std::thread(jemallocTest, count, size[i]);
}
for (auto& t : threads) {
t.join();
}
// Dump allocator statistics to stderr.
//malloc_stats_print(NULL, NULL, NULL);
getchar();
return 0;
}
测试结果:着实可以提高堆内存申请和释放效率。
使用方法:
#include "jemalloc/jemalloc.h"
//#define JEMALLOC_NO_DEMANGLE
//#define JEMALLOC_NO_DEMANGLE //内存检测
#ifdef USE_JEMALLOC
//C redefine malloc
#define malloc(size) je_malloc(size)
#define calloc(count,size) je_calloc(count,size)
#define realloc(ptr,size) je_realloc(ptr,size)
#define free(ptr) je_free(ptr)
//C++ redefine new/delete
void* operator new(std::size_t size, const char* file, int line) {
return je_malloc(size);
}
void* operator new[](std::size_t size, const char* file, int line) {
return je_malloc(size);
}
/*
void* operator new(std::size_t size, const std::nothrow_t&) {
return je_malloc(size);
}
void* operator new[](std::size_t size, const std::nothrow_t&) {
return je_malloc(size);
}
*/
//#define new new(__FILE__, __LINE__)
#define SAFE_NEW new(__FILE__, __LINE__)
#ifndef SAFE_DELETE
#define SAFE_DELETE(p) do {if (p) {je_free(p); (p) = NULL;}} while(0)
#endif
#ifndef SAFE_DELETE_ARRAY
#define SAFE_DELETE_ARRAY(p) do {if (p) {je_free(p); (p) = NULL;}} while(0)
#endif
#endif