473,837 Members | 1,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL and pooling memory

Hi all,
Does anybody know if STLPort or SGI STL standard allocators do memory
pooling for the list, map and set?
Also I have had a look at the BOOST pool_alloc (to be used as a pooling
allocator for lists), but looking into the code it doesn't seem to ever
release (to the global ::free) the memory that was once allocated.

I can understand that such memory can be re-used if I have another list
of the same object types later in the program, but it seems to me that
such pool_alloc should anyway release blocks which happen to be
completely free (*) otherwise the program will always appear to have the
peak memory allocation to the operating system. Like leaking! Doesn't it
really ever free the blocks, or it's my overlook?
(*) there are likely to be many totally free blocks after you finish
working with a bunch of pooled lists, if the implementation is what it
seemed to me: every new alloc call always returns the first free slot:
it's an autodefragmenti ng algorithm
Jul 22 '05 #1
11 2668
aaaaa wrote:
Hi all,
Does anybody know if STLPort or SGI STL standard allocators do memory
pooling for the list, map and set?
I recall that SGI had a web page where they explained that they do some
pooling in their allocators. I do not know about STLPort.
Also I have had a look at the BOOST pool_alloc (to be used as a pooling
allocator for lists), but looking into the code it doesn't seem to ever
release (to the global ::free) the memory that was once allocated.

I can understand that such memory can be re-used if I have another list
of the same object types later in the program, but it seems to me that
such pool_alloc should anyway release blocks which happen to be
completely free (*) otherwise the program will always appear to have the
peak memory allocation to the operating system. Like leaking! Doesn't it
really ever free the blocks, or it's my overlook?


Are you sure that a call to free() will release the memory to your
operating system. As far as I understand the language of the standard, the
only guarantee given for free() is that a deallocated region will be
available to malloc() calls from the same program. (Actually, the C
standard, from which C++ inherits free(), can be read so that the
postcondition of free() actually is that the memory will be available for
subsequent calls to malloc() in which case the memory must not be returned
to the operating system.)

On my system I just used the following code to check the behavior of free.
I conforms the the interpretation of the standard that I outlined: at no
point, memory is returned to the operating system.
#include <list>
#include <iostream>
#include <string>

typedef char page [1024];
typedef page* page_ptr;

int main ( void ) {
std::list< page_ptr > ptr_list;
std::cout << "allocating 300 MB of memory.\n";
for ( unsigned int i = 0; i < 300000; ++i ) {
ptr_list.push_b ack( reinterpret_cas t< page_ptr >( malloc( sizeof( page
) ) ) );
}

{
std:: cout << "allocation done, hit enter for deallocation.";
std::string line;
std::getline( std::cin, line );
std::cout << "\n";
}

while( ! ptr_list.empty( ) ) {
free( ptr_list.back() );
ptr_list.pop_ba ck();
}

{
std:: cout << "deallocati on done, hit.";
std::string line;
std::getline( std::cin, line );
std::cout << "\n";
}

std::cout << "allocating 300 MB of memory again.\n";
for ( unsigned int i = 0; i < 300000; ++i ) {
ptr_list.push_b ack( reinterpret_cas t< page_ptr >( malloc( sizeof( page
) ) ) );
}

{
std:: cout << "allocation done, hit enter for deallocation.";
std::string line;
std::getline( std::cin, line );
std::cout << "\n";
}

while( ! ptr_list.empty( ) ) {
free( ptr_list.back() );
ptr_list.pop_ba ck();
}

{
std:: cout << "hit enter to quit.";
std::string line;
std::getline( std::cin, line );
std::cout << "\n";
}

}
If you really need to return memory to the operating system, you will have
to write your own allocator and use system calls, which are system specific
and considered off topic in this group.
Best

Kai-Uwe Bux
Jul 22 '05 #2
aaaaa wrote:

I can understand that such memory can be re-used if I have another list
of the same object types later in the program, but it seems to me that
such pool_alloc should anyway release blocks which happen to be
completely free (*) otherwise the program will always appear to have the
peak memory allocation to the operating system. Like leaking! Doesn't it
really ever free the blocks, or it's my overlook?


You might want to look at our CoreX package, which has a toolkit for
building custom allocators. You can write allocators that sequester
their memory, so that it's not available to the rest of the application,
and you can write allocators that release their memory.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #3
On Thu, 16 Sep 2004 21:16:10 -0500, aaaaa <aa***@aaa.co m> wrote:
Hi all,
Does anybody know if STLPort or SGI STL standard allocators do memory
pooling for the list, map and set?
Yes, they do, and it's configurable with #defines (but on by default
IIRC). Read the docs.
Also I have had a look at the BOOST pool_alloc (to be used as a pooling
allocator for lists), but looking into the code it doesn't seem to ever
release (to the global ::free) the memory that was once allocated.
That sounds correct. Note that you should be using fast_pool_alloc ator
for std::list, since I think it's faster for unit allocations such as
used by list.

You can manually free slack space by using the underlying singleton
pool allocator. Something like:

singleton_pool< fast_pool_alloc ator_tag,
size_of_list_no de>::release_me mory();

This hitch is working out the size of a list node portably, and I'm
not sure there's a way of doing it. You could guess like this:

template <class T>
struct list_node_sizer
{
test* pre;
test* post;
T t;
};

singleton_pool< fast_pool_alloc ator_tag,
sizeof(list_nod e_sizer<MyType> )>::release_mem ory();

I can understand that such memory can be re-used if I have another list
of the same object types later in the program, but it seems to me that
such pool_alloc should anyway release blocks which happen to be
completely free (*) otherwise the program will always appear to have the
peak memory allocation to the operating system. Like leaking! Doesn't it
really ever free the blocks, or it's my overlook?


If you want to control memory usage, it might be better to use a
non-singleton allocator. boost don't have one, but I'm sure one can be
written based on what they've got - the hardest thing is handling
rebound copies of the allocator.

Tom
Jul 22 '05 #4
Kai-Uwe Bux wrote:
aaaaa wrote:

Are you sure that a call to free() will release the memory to your
operating system.
On my system I just used the following code...


DOH!?!?
REALLY!?!?

Excuse me, you are telling me that to the OS a C++ program will always
appear as occupying as much memory as its peak usage?

Anybody else can please confirm?!
All the applications I remember of, under Windows (my OS) do appear to
decrease the memory usage sometimes, and I'm pretty sure that most of
them are written in C or C++.
Jul 22 '05 #5
aaaaa wrote:

Excuse me, you are telling me that to the OS a C++ program will always
appear as occupying as much memory as its peak usage?


Not always, but often.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #6
aaaaa wrote:
Kai-Uwe Bux wrote:
aaaaa wrote:

Are you sure that a call to free() will release the memory to your
operating system.
On my system I just used the following code...
DOH!?!?
REALLY!?!?

Excuse me, you are telling me that to the OS a C++ program will always
appear as occupying as much memory as its peak usage?


No.

What I was saying, in the part you snipped, was that free() does not have
to return memory to the OS. Of course, the OS offers system calls for
programs to obtain and return memory. However, these are system specific,
not known to me, and not covered in the C or C++ standard. You will be
better off to try in news group for your platform.

Anybody else can please confirm?!
All the applications I remember of, under Windows (my OS) do appear to
decrease the memory usage sometimes, and I'm pretty sure that most of
them are written in C or C++.


And I am pretty sure, they use appropriate system calls to achieve that,
unless free() does some non-standard magic in the C/C++ library that ships
with your Windows version. You can modify the code I provided to see for
yourself what happens on your machine. I do not use Windows, and actually,
I would be interested in your results.
Best

Kai-Uwe Bux
Jul 22 '05 #7
Ok I understand now, thanks.

I tried your code: it does return it to the OS (windows 2000): the
"VM-Size" in the task manager drops to ~0 during deallocation.

However I noticed that it takes an incredible amount of time to
deallocate! Just a few seconds to allocate 300M but something like 5
minutes to deallocate it!!

This was with VC++7 native STL. I might give a try to STLPort in the
future: if it pools the memory it should be faster. However, your
independent mallocs and frees would not be affected anyway of course.

Now really I see why memory pools and the copying garbage collectors are
useful.
Jul 22 '05 #8
Tom Widmer wrote:
On Thu, 16 Sep 2004 21:16:10 -0500, aaaaa <aa***@aaa.co m> wrote:
Hi all,
Does anybody know if STLPort or SGI STL standard allocators do memory
pooling for the list, map and set?


Yes, they do, and it's configurable with #defines (but on by default
IIRC). Read the docs.

Would you point me to such "docs" because I don't appear to be able to
find them

Thanks
Jul 22 '05 #9
aaaaa <aa***@aaa.co m> writes:
Tom Widmer wrote:
On Thu, 16 Sep 2004 21:16:10 -0500, aaaaa <aa***@aaa.co m> wrote:
Hi all,
Does anybody know if STLPort or SGI STL standard allocators do
memory pooling for the list, map and set?

Yes, they do, and it's configurable with #defines (but on by default
IIRC). Read the docs.

Would you point me to such "docs" because I don't appear to be able to
find them


http://www.sgi.com/tech/stl/table_of_contents.html
http://www.stlport.org/doc/index.html

HTH
Nicolas

P.S. There is something called google :-)
--
| Nicolas Pavlidis | Elvis Presly: |\ |__ |
| Student of SE & KM | "Into the goto" | \|__| |
| pa****@sbox.tug raz.at | ICQ #320057056 | |
|-------------------University of Technology, Graz----------------|
Jul 22 '05 #10

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

Similar topics

2
8528
by: Steve Jenkins | last post by:
Wonder if anyone can help. So, I've read: >> http://uk2.php.net/function.mysql-pconnect >> http://uk2.php.net/manual/en/features.persistent-connections.php Can one seriously see persistent connections as a form of db connection pooling? Is it really similar? Thanks for any clarification from anyone,
7
1842
by: Walter Zydhek | last post by:
Which would be less resource instensive and faster? A "hosted" remoting class or a class set up with object pooling via Component Services? -Walt Zydhek
1
5732
by: Lenny Shprekher | last post by:
Hi, I am getting issues that Oracle collecting opened sessions (connections) from my webservice using regular System.Data.OleDb.OleDbConnection object. I am guessing that this is connection pooling issue. Is there is any way to disable connection pooling for one particular .net webservice? Thanks, Leonid
4
14635
by: Susan Baker | last post by:
PHP Newbie here - I apologize if any of my questions appear daft or obvious ... Does PHP (or maybe the web server - Apache in my case), support (database) connection pooling?. It seems terribly inefficient if every request for data wil incurr the overhead of creating a connection to the db. While on the subject of pooling - does any one know (when using PHP in "server side scripting") if scripts are launched as seperate processes
3
2205
by: nebiyou1 | last post by:
I am trying to implement a simple memory pooling (simple segregated storage) for character strings (char*) of fixed size. I am running into some issues, however. Here is an oversimplified example: The following allocates a space to hold chunk number of char's. It then builds a link list and adds the chunks to the list.
4
1837
by: bob | last post by:
Hi, can anybody tell me whats the most widely used/trusted/tested and free memory pooling solution out there? I'd like to download it and possibly introduce it into our application (problems with too much time spent new'ing and delete'ing). thanks G
16
2883
by: crbd98 | last post by:
Hello All, Some time ago, I implemented a data access layer that included a simple connectin pool. At the time, I did it all by myself: I created N connections, each connection associated with a worker thread that would execute the db commands. The pool was fixed and all the connections were created when the db access class was instantiated. The connections remained opened during the whole execution. If a connection was not available...
9
4770
by: fniles | last post by:
I am using VB.NET 2003 and SQL 2005. To use connection pooling and avoid the error "There is already an open DataReader associated with this Connection which must be closed first." , I understand that I want to release/close connections in a timely fashion. What I do is I declare the SqlClient.SqlConnection variable as a local variable inside my subroutine (instead of a global variable). Right before I Fill the dataset I open the...
0
6629
viswarajan
by: viswarajan | last post by:
Introduction This article is to go in deep in dome key features in the ADO.NET 2 which was shipped with VS 2005. In this article I will go trough one of the key features which is the Connection Pooling. This feature is a key feature plays an important role in the performance in most of business application or Data driven application. What's Connection Pooling?
0
9839
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9682
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,...
1
10621
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,...
0
10268
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9396
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6998
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
5846
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4039
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3123
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.