473,756 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to implement a buffer (memory?) pool

As part of applying for a programming position at a company, I
recently I had submitted some code samples to one of the developers
for review.

This is the feedback I received:

One of his concerns was frequent calls to new and delete, which can
cause
memory fragmentation over time. An example is the allocation and
destruction
of a memory buffer for every network packet transmission, vs.
employing a
buffer pool.

I assume a "buffer pool" is the same thing as a memory pool.

Could anyone please direct me to resources that would provide me with
information on how to implement a buffer/memory pool especially for
the use of transmitting network packets.
Jul 22 '05 #1
8 6860

"Tron Thomas" <tr*********@ve rizon.net> wrote in message
news:a4******** *************** ***@posting.goo gle.com...
As part of applying for a programming position at a company, I
recently I had submitted some code samples to one of the developers
for review.

This is the feedback I received:

One of his concerns was frequent calls to new and delete, which can
cause
memory fragmentation over time. An example is the allocation and
destruction
of a memory buffer for every network packet transmission, vs.
employing a
buffer pool.

I assume a "buffer pool" is the same thing as a memory pool.

Could anyone please direct me to resources that would provide me with
information on how to implement a buffer/memory pool especially for
the use of transmitting network packets.


Allocate enough memory "once" and then use it each time. If you have a
situation where multiple buffers are needed, then you could create 5 (or
however many) of them, store the address, and store when it's active or not
(being used). It all depends on how many you need at the same time.

Then, when you need a buffer, you call a function which gets a buffer
from this pool, when you are done with it, you call a function that marks
the area as free again. When that all works you can contemplate overriding
new/delete for a specific class of "network buffer" class.

Sometimes you need a variable number of things, with unknown limits.
That can get interesting as your type of storage for the list has to be of a
method that doesn't fragment memory after time (as you resize your
containers that hold the pointers). Of course, it's up to your OS and
implementation on how it does things also. Last time I googled for a
memory pool class I came up with nothing useful (to me)

I believe there is a reason for that:

There's usually a specific type of memory pool that works for a
specific situation, custom code type thing. There may be a blanket
solution these days, but I doubt it would fit every possible use. I must
also mention that this is a very important/core feature of any program that
intends to run very long.

You should write some sample programs (test programs) and see what the
address of things are after new/deleteing a bunch of things: Such as

new a
new b
new c
delete b
new d
.. Keep doing this...

Then see where the object lie. It's interesting to see how garbled it
can get after a while.

Good luck.




Jul 22 '05 #2
"c wood" <re******@veriz on.net> wrote in message news:<ne******* *********@nwrdd c02.gnilink.net >...
Allocate enough memory "once" and then use it each time. If you have a
situation where multiple buffers are needed, then you could create 5 (or
however many) of them, store the address, and store when it's active or not
(being used). It all depends on how many you need at the same time.

Then, when you need a buffer, you call a function which gets a buffer
from this pool, when you are done with it, you call a function that marks
the area as free again. When that all works you can contemplate overriding
new/delete for a specific class of "network buffer" class.

Sometimes you need a variable number of things, with unknown limits.
That can get interesting as your type of storage for the list has to be of a
method that doesn't fragment memory after time (as you resize your
containers that hold the pointers). Of course, it's up to your OS and
implementation on how it does things also. Last time I googled for a
memory pool class I came up with nothing useful (to me)

I believe there is a reason for that:

There's usually a specific type of memory pool that works for a
specific situation, custom code type thing. There may be a blanket
solution these days, but I doubt it would fit every possible use. I must
also mention that this is a very important/core feature of any program that
intends to run very long.

You should write some sample programs (test programs) and see what the
address of things are after new/deleteing a bunch of things: Such as

new a
new b
new c
delete b
new d
.. Keep doing this...

Then see where the object lie. It's interesting to see how garbled it
can get after a while.

Good luck.


I understand the general concept of a memory pool. What I'm not sure
of is how I would want to actually implement one.

Allocating a buffer to instantiate objects doesn't seem to prevent the
problem of fragmentation. The fragmentation will now be localized to
the allocated buffer and not the global heap. This fragmentation
seems to be what the developer who offered feedback I stated in my
original post was most concerned about.

I can appreciate the fact that a memory pool implementation may be
specific to the context it is used. Seeing different implementation
in different contexts would still be useful for letting me know how I
would want to do my implementation.

Where could I find some examples of implemented memory pools?
Jul 22 '05 #3
"Tron Thomas" <tr*********@ve rizon.net> wrote in message
news:a4******** *************** ***@posting.goo gle.com...
"c wood" <re******@veriz on.net> wrote in message

news:<ne******* *********@nwrdd c02.gnilink.net >...
Allocate enough memory "once" and then use it each time. If you have a situation where multiple buffers are needed, then you could create 5 (or
however many) of them, store the address, and store when it's active or not (being used). It all depends on how many you need at the same time.

Then, when you need a buffer, you call a function which gets a buffer from this pool, when you are done with it, you call a function that marks the area as free again. When that all works you can contemplate overriding new/delete for a specific class of "network buffer" class.

Sometimes you need a variable number of things, with unknown limits. That can get interesting as your type of storage for the list has to be of a method that doesn't fragment memory after time (as you resize your
containers that hold the pointers). Of course, it's up to your OS and
implementation on how it does things also. Last time I googled for a
memory pool class I came up with nothing useful (to me)

I believe there is a reason for that:

There's usually a specific type of memory pool that works for a
specific situation, custom code type thing. There may be a blanket
solution these days, but I doubt it would fit every possible use. I must also mention that this is a very important/core feature of any program that intends to run very long.

You should write some sample programs (test programs) and see what the address of things are after new/deleteing a bunch of things: Such as

new a
new b
new c
delete b
new d
.. Keep doing this...

Then see where the object lie. It's interesting to see how garbled it can get after a while.

Good luck.


I understand the general concept of a memory pool. What I'm not sure
of is how I would want to actually implement one.

Allocating a buffer to instantiate objects doesn't seem to prevent the
problem of fragmentation. The fragmentation will now be localized to
the allocated buffer and not the global heap. This fragmentation
seems to be what the developer who offered feedback I stated in my
original post was most concerned about.

Well, look at it this way: The memory pool allocates a block of memory
when needed. If a second block is needed, it allocates that one as well. Now
the first one is freed and after that another one is requested. Most of the
time, you would get a completely new block of memory now. Repeat this and
you get the fragmentation. But now, your memory pool manager class (or
whatever) does not allocate a new one, but first checks if a previously
freed block (in your 'pool' of memory blocks) is large enough to fulfill the
allocation request. If so, you can avoid allocating new memory.

hth
--
jb

(replace y with x if you want to reply by e-mail)
Jul 22 '05 #4

"Tron Thomas" <tr*********@ve rizon.net> wrote in message
news:a4******** *************** ***@posting.goo gle.com...
"c wood" <re******@veriz on.net> wrote in message news:<ne******* *********@nwrdd c02.gnilink.net >... I understand the general concept of a memory pool. What I'm not sure
of is how I would want to actually implement one.

Allocating a buffer to instantiate objects doesn't seem to prevent the
problem of fragmentation. The fragmentation will now be localized to
the allocated buffer and not the global heap. This fragmentation
seems to be what the developer who offered feedback I stated in my
original post was most concerned about.

I can appreciate the fact that a memory pool implementation may be
specific to the context it is used. Seeing different implementation
in different contexts would still be useful for letting me know how I
would want to do my implementation.

Where could I find some examples of implemented memory pools?


Try www.google.com with the search for "C++" memory pool

But like I said, there isn't going to be a cure all that fits your
situation unless you tell us the situation.

Answer these:

How many will you need at one time (MAX)?.
How many on average will you need?
What size is the buffer or is it variable, if so, what size range
in bytes?

Your answers to those 3.5 questions make a huge difference on what would
work for you.

You could also google for memory allocation algorithms, but that's
the worst case scenario.

/* OFF TOPIC: Please also note that some OS's have such mechanisms
built into them. */




Jul 22 '05 #5
Jakob Bieling wrote:
"Tron Thomas" <tr*********@ve rizon.net> wrote in message
news:a4******** *************** ***@posting.goo gle.com...
"c wood" <re******@veriz on.net> wrote in message

Allocating a buffer to instantiate objects doesn't seem to prevent the
problem of fragmentation. The fragmentation will now be localized to
the allocated buffer and not the global heap. This fragmentation
seems to be what the developer who offered feedback I stated in my
original post was most concerned about.


Well, look at it this way: The memory pool allocates a block of memory
when needed. If a second block is needed, it allocates that one as well. Now
the first one is freed and after that another one is requested. Most of the
time, you would get a completely new block of memory now. Repeat this and
you get the fragmentation.


Maybe, maybe not, on some implementations if you deallocate
20 bytes and then allocate 20 bytes you'll get back the same
memory pointer.

But now, your memory pool manager class (or
whatever) does not allocate a new one, but first checks if a previously
freed block (in your 'pool' of memory blocks) is large enough to fulfill the
allocation request. If so, you can avoid allocating new memory.


That is usually how memory allocators are implemented
anyway, they mostly avoid growing the address space
allocated. Fragmentation occurs because memory allocations
are released in between other memory allocations and you end
up with small holes of freed memory:

=== ==== == == ==== == ===== ===== ==== ====

the memory pool doesn't eliminate this fragmentation but
limits its scope:

memory pool
=== ==== == ==-====-==--==== ===== ==== ====

the fragmentation due to the packet allocations doesn't
spread outside of the block set aside for this purpose.

Jul 22 '05 #6
"lilburne" <li******@godzi lla.net> wrote in message
news:br******** *****@ID-203936.news.uni-berlin.de...
Jakob Bieling wrote:

But now, your memory pool manager class (or
whatever) does not allocate a new one, but first checks if a previously
freed block (in your 'pool' of memory blocks) is large enough to fulfill the allocation request. If so, you can avoid allocating new memory.


That is usually how memory allocators are implemented
anyway, they mostly avoid growing the address space
allocated. Fragmentation occurs because memory allocations
are released in between other memory allocations and you end
up with small holes of freed memory:

=== ==== == == ==== == ===== ===== ==== ====

the memory pool doesn't eliminate this fragmentation but
limits its scope:

memory pool
=== ==== == ==-====-==--==== ===== ==== ====

the fragmentation due to the packet allocations doesn't
spread outside of the block set aside for this purpose.

The only other solution would be t allocate one llarger block of memory
and return parts of it to the caller. But as the OP pointed out already,
this would only move fragmentation from the heap to the memory pool, unless
the allocation sizes are always the same (which, in the OPs solution is very
unlikely to be the case).

Unless you have a different approach, I do not see why I should go thru
the extra effort of managing my own memory block, when I can let the heap do
this and still end up with the same fragmentation.

regards
--
jb

(replace y with x if you want to reply by e-mail)
Jul 22 '05 #7
c wood wrote:
Try www.google.com with the search for "C++" memory pool

But like I said, there isn't going to be a cure all that fits your
situation unless you tell us the situation.

Answer these:

How many will you need at one time (MAX)?.
How many on average will you need?
What size is the buffer or is it variable, if so, what size range
in bytes?

Your answers to those 3.5 questions make a huge difference on what would
work for you.

You could also google for memory allocation algorithms, but that's
the worst case scenario.

/* OFF TOPIC: Please also note that some OS's have such mechanisms
built into them. */


I tried the search you suggested on Google, and it didn't reveal
anything that was more useful than any other searches I've already tried

The program I'm working on is a two player networked game that sends
messages between two systems. I modified my code to spit out how much
memory the program was consuming while it was sending messages.

Here are some of the results I got:

0 bytes allocated for message use.
20 bytes allocated for message use.
28 bytes allocated for message use.
8 bytes allocated for message use.
64 bytes allocated for message use.
108 bytes allocated for message use.
52 bytes allocated for message use.
8 bytes allocated for message use.
0 bytes allocated for message use.
44 bytes allocated for message use.
88 bytes allocated for message use.
128 bytes allocated for message use.
88 bytes allocated for message use.
44 bytes allocated for message use.
84 bytes allocated for message use.
44 bytes allocated for message use.
0 bytes allocated for message use.
56 bytes allocated for message use.
100 bytes allocated for message use.
44 bytes allocated for message use.
0 bytes allocated for message use.
44 bytes allocated for message use.
88 bytes allocated for message use.
128 bytes allocated for message use.
88 bytes allocated for message use.
44 bytes allocated for message use.
84 bytes allocated for message use.
44 bytes allocated for message use.
0 bytes allocated for message use.
....
0 bytes allocated for message use.
44 bytes allocated for message use.
88 bytes allocated for message use.
128 bytes allocated for message use.
172 bytes allocated for message use.
216 bytes allocated for message use.
236 bytes allocated for message use.
244 bytes allocated for message use.
224 bytes allocated for message use.
280 bytes allocated for message use.
324 bytes allocated for message use.
268 bytes allocated for message use.
228 bytes allocated for message use.
184 bytes allocated for message use.
224 bytes allocated for message use.
184 bytes allocated for message use.
140 bytes allocated for message use.
180 bytes allocated for message use.
136 bytes allocated for message use.
192 bytes allocated for message use.
236 bytes allocated for message use.
180 bytes allocated for message use.
140 bytes allocated for message use.
96 bytes allocated for message use.
136 bytes allocated for message use.
96 bytes allocated for message use.
52 bytes allocated for message use.
44 bytes allocated for message use.
88 bytes allocated for message use.
132 bytes allocated for message use.
172 bytes allocated for message use.
228 bytes allocated for message use.
272 bytes allocated for message use.
216 bytes allocated for message use.
176 bytes allocated for message use.
132 bytes allocated for message use.
172 bytes allocated for message use.
132 bytes allocated for message use.
88 bytes allocated for message use.
44 bytes allocated for message use.
0 bytes allocated for message use.
....
572 bytes allocated for message use.

The last entry in the results is out of context and it is included
because it was the largest value I found when looking at the output.

Does the output go towards answering the questions you posed in your post?

Jul 22 '05 #8
I have create an implementation for a memory pool, and it seems to
work. Would it be practical for me to post the implmentation here for
people to review and let me know if it seems like it would be
effective for what I want to accomplish?

tr*********@ver izon.net (Tron Thomas) wrote in message news:<a4******* *************** ****@posting.go ogle.com>...
As part of applying for a programming position at a company, I
recently I had submitted some code samples to one of the developers
for review.

This is the feedback I received:

One of his concerns was frequent calls to new and delete, which can
cause
memory fragmentation over time. An example is the allocation and
destruction
of a memory buffer for every network packet transmission, vs.
employing a
buffer pool.

I assume a "buffer pool" is the same thing as a memory pool.

Could anyone please direct me to resources that would provide me with
information on how to implement a buffer/memory pool especially for
the use of transmitting network packets.

Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2378
by: Mark | last post by:
If you have STL containers (like list, vector,...) in functions as automatic variables, do the nodes that are put on the containers get buffered so they can be reused by later container operations? For example, if I have 10 member methods, and lots of them have a list <Foo*> when that list is cleared or goes out of scope, the nodes are deleted (don't worry about deleting the Foo* themselves, they are just extra pointers to classes in...
7
11417
by: eric | last post by:
Hi there, BP hit ratio = 1 - (BP physical reads / BP logical reads). If all the BP physical reads are asynchronous, it should mean that the pages are brought up to the bufferpool before the database manager needs them. But in that case, the BP hit ratio will be 0% (BP physical reads = BP async. physical reads = BP logical reads). Could this formula BP hit ratio = 1 - ((BP physical reads - BP async.
36
4358
by: xixi | last post by:
hi, we are using db2 udb v8.1 on windows, i try to use the configuration advisor to get recommendation on the parameter setting, at first, i give the target memory for DB2 is 80% of the physical memory, it gives me a recommendation on bufferpool size, so i accept the change, later on , i found that i gave too much memory to DB2, so i change the target lower, but the recommendation doesn't change the buffer pool size accordingly, why is...
6
3988
by: Mark | last post by:
I understand the concept of catalog cache (memory allocated from the dbheap to allow catalog lookups without the need to access disk each time). But the DB2 catalog tablespace (SYSCATSPACE) is assigned a buffer pool, which seems to perform the same function as catalog cache. Is this simply a case of double buffering? Can anyone clear this up?
3
5418
by: Mark | last post by:
In a DB2 V8.1 performance tuning document from a 3rd party vendor, I found this statement. Can anyone verify this? "DB2 requires 100 bytes of memory for every buffer pool and extended storage page that is allocated to a database for use as a descriptor in the database heap, so the size of the DBHEAP configuration parameter should be considered before creating large buffer pools. For example, a 1 gigabyte buffer pool with a page size of 4...
6
6620
by: xeqister | last post by:
Greetings, We are having a situation here whereby one of our staff created a very huge 32K buffer pool in a production database and caused the database to go down. When we try to reconnect to the database using "db2 connect to <dbname>", its giving the following error: SQL1224N A database agent could not be started to service a request, or was terminated as a result of a database system shutdown or a force command. SQLSTATE:55032
6
2403
by: Angel Tsankov | last post by:
Hi, I remember reading in a book (or in an article) that the optmial buffer growth factor is about 1.6. Now I need to find this book but I can't remember its title. Can someone help me with this?
4
6024
by: Patrick Finnegan | last post by:
Is there a DB2 setting that will force a table to be cached in the buffer pool? We have four tables that we want to cache completely in the buffer pool to ensure that all the data is read from memory. The buffer pool ratio should be close to 100%. It's about 80% at the moment. Would a "select all" statement cache everything in the table?
0
9287
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9886
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9857
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7259
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6542
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2677
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.