Performance: Virtual Allocation vs Heap Allocation

Virtual Allocation is slower than Heap allocation because heap manager is well optimized for small memory block allocations. So, how fast is it? Here is the benchmark.

The first test (List 1) is allocating 0KB to 512KB memory block 5,000,000 times through VirtualAlloc and HeapAlloc. The result is somewhat expected – Heap allocation is about 2.5 times faster than Virtual Allocation.

 

image

 

The second test (List 2) runs the same code in two different threads at the same time. The Windows Heap manager implements well-optimized multithread memory allocation called front-end heap. The performance difference is larger now – heap allocation is 3.8 times faster than virtual allocation.

 

image

 

// ----------------
//  List - 1
// ----------------
using namespace std;

int main(){

boost::timer tim;

deque< void* > blockList;
for(size_t i = 0; i < 5 * 1000 * 1000; i++){
size_t size = rand() % (512 * 1024);

void* p = VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
blockList.push_front(p);

if(blockList.size() > 10){
void* p = blockList.back();
blockList.pop_back();

VirtualFree(p, 0, MEM_RELEASE);
}
}

cout << "Virtual Allocation: " << tim.elapsed() << " seconds" << endl;
blockList.clear();

tim.restart();
for(size_t i = 0; i < 5 * 1000 * 1000; i++){
size_t size = rand() % (512 * 1024);

void* p = malloc(size);
blockList.push_front(p);

if(blockList.size() > 10){
void* p = blockList.back();
blockList.pop_back();

free(p);
}
}
cout << "Heap Allocation: " << tim.elapsed() << " seconds" << endl;

return 0;
}

 

 

// ----------------
//  List - 2
// ----------------
using namespace std;

void VirtualAllocProc(){
deque< void* > blockList;
for(size_t i = 0; i < 5 * 1000 * 1000; i++){
size_t size = rand() % (512 * 1024);

void* p = VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
blockList.push_front(p);

if(blockList.size() > 10){
void* p = blockList.back();
blockList.pop_back();

VirtualFree(p, 0, MEM_RELEASE);
}
}
}

void HeapAllocProc(){
deque< void* > blockList;
for(size_t i = 0; i < 5 * 1000 * 1000; i++){
size_t size = rand() % (512 * 1024);

void* p = malloc(size);
blockList.push_front(p);

if(blockList.size() > 10){
void* p = blockList.back();
blockList.pop_back();

free(p);
}
}
}

int main(){
{
boost::timer tim;
boost::thread th1(VirtualAllocProc);
boost::thread th2(VirtualAllocProc);

th1.join();
th2.join();
cout << "Virtual Allocation: " << tim.elapsed() << " seconds" << endl;
}

{
boost::timer tim;
boost::thread th1(HeapAllocProc);
boost::thread th2(HeapAllocProc);

th1.join();
th2.join();
cout << "Heap Allocation: " << tim.elapsed() << " seconds" << endl;
}
}

About Moto

Engineer who likes coding
This entry was posted in Windows Memory Management. Bookmark the permalink.

2 Responses to Performance: Virtual Allocation vs Heap Allocation

  1. Akihiro says:

    RAND_MAX may be less than 512*1024. You should check the allocation size.

  2. Motonari says:

    Akihiro san, you are correct. It’s an embarrassing mistake.

Leave a reply to Akihiro Cancel reply