Boost logo

Boost Users :

From: 蹬三轮的 (tricycle_at_[hidden])
Date: 2007-09-06 22:39:34


> We use boost pool in our project and we found boost's pool may occupy
> too much memory. Say, if we need a block of memory which size is
> 1317B, boost's pool will allocate a truck of memory 5268B on a 32bits
> platform, leave 3951 bytes unused (it get worse on 64bits platform). I
> don't think it is necessary to allocate 5268B, for the sake of
> performance, only 1320B is enough for align. It should be resovled
> because pool is so important to many large application.

pool allocates several blocks of memory at once, then divvies them
out. Nothing is wasted. Or did you have something else in mind?

--
Cory Nelson
Hi Nelson, thanks for your reply.
According to the description of pool, it should be working like you
said. But actually it is not the same. Hereby, there are two cases
provided.
I think you will find two totally different results between them.
1. blocksize = 1316
Everything is ok. .
2. blocksize =1317
Disaster! You can notice the waste allocated memory. I just want case
2 to run like case 1.
Following is the code, you can check it.
1.
i:   0 need:    41(KB) alloc:    41(KB) waste:      8(B)
i:  32 need:    82(KB) alloc:    82(KB) waste:      8(B)
i:  96 need:   164(KB) alloc:   164(KB) waste:      8(B)
i: 224 need:   329(KB) alloc:   329(KB) waste:      8(B)
i: 480 need:   658(KB) alloc:   658(KB) waste:      8(B)
i: 992 need:     1(MB) alloc:     1(MB) waste:      8(B)
i:2016 need:     2(MB) alloc:     2(MB) waste:      8(B)
i:4064 need:     5(MB) alloc:     5(MB) waste:      8(B)
i:8160 need:    10(MB) alloc:    10(MB) waste:      8(B)
2.
i:   0 need:    41(KB) alloc:   164(KB) waste:   123(KB)
i:  32 need:    82(KB) alloc:   329(KB) waste:   246(KB)
i:  96 need:   164(KB) alloc:   658(KB) waste:   493(KB)
i: 224 need:   329(KB) alloc:     1(MB) waste:   987(KB)
i: 480 need:   658(KB) alloc:     2(MB) waste:     1(MB)
i: 992 need:     1(MB) alloc:     5(MB) waste:     3(MB)
i:2016 need:     2(MB) alloc:    10(MB) waste:     7(MB)
i:4064 need:     5(MB) alloc:    20(MB) waste:    15(MB)
i:8160 need:    10(MB) alloc:    41(MB) waste:    30(MB)
here is the sample code:
#include <iostream>
#include <boost/pool/pool.hpp>
#include <iomanip>
#include <boost/lexical_cast.hpp>
using namespace std;
int i = 0;
int nextSize = 0;
int blockSize = 1317;
string sizeFormat(int n)
{
	if (n < 1024)
	{
		return boost::lexical_cast <string> (n) + "(B)";
	}
	else if (n < 1048576)
	{
		return boost::lexical_cast <string> (n / 1024) + "(KB)";
	}
	else
	{
		return boost::lexical_cast <string> (n / 1048576) + "(MB)";
	}
}
struct MyAllocator
{
	typedef std::size_t size_type;
	typedef std::ptrdiff_t difference_type;
	static char * malloc(const size_type bytes)
	{
		int need = nextSize * blockSize;
		int waste = bytes - need;
		cout << "i:" << setw(4) << i
			<< " need:" << setw(10) << sizeFormat(nextSize * blockSize)
			<< " alloc:" << setw(10) << sizeFormat(bytes)
			<< " waste:" << setw(10) << sizeFormat(waste) << endl;
		return new (std::nothrow) char[bytes];
	}
	static void free(char * const block)
	{
		delete [] block;
	}
};
int main()
{
	//typedef std::vector <int, MA <int> > MYVector;
	boost::pool <MyAllocator> intPool(blockSize);
	for (i = 0; i < 10000; ++i)
	{
		nextSize = intPool.get_next_size();
		intPool.malloc();
	}
}

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net