473,386 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

STL map and memory management

3
Hi,
I've a map of maps like the following:

map <int, map <int, map <string, int> > > mymap;

that, inside a big loop, use a lot of memory so that the entire computer memory is used. I would like to free the memory sometimes inside the loop (when I'm sure that some keys are not used anymore) but using clear() the map is empty but the objects are still in memory.

thanks in advance!

Pierpaolo
May 22 '07 #1
5 3115
weaknessforcats
9,208 Expert Mod 8TB
You may need to swap your empty map with a new one that has no elements and then copy back to the original.

STL containers do not always release memory when elements are removed.

Check out: Scott Meyers book Effective STL.
May 22 '07 #2
paulix
3
Please could you be more precise?
My code is something like:

map <int, map <int, map <string, int> > > mymap;
for(int l=0; l<nline; l++) {
....................
mymap[..][..][..]++;

if(some conditions){
for(map...iterator h mymap.begin(); h != mymap.end(); h++){
............
some print
}
}
}

after the print I should have to free the memory (for some keys) but I'm not able to obtain that using clear(). Not only....I noticed (using the linux command 'top') that when I'm inside the second loop (the one that use the iterator to have access to the map elements) the memory used it is doubled...as if the map and all its elements are copied into a second map in memory!
May 22 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
You are not in control of the container. You cannot tell the container to delete memory just because you deleted an element. You will have to create an empty container and swap it withe the full one.

OR:

Do not use objects in the map. Use a reference counted handle instread and have a map of handles. Handles are objects of an envelope class and somes times called smart, or managed pointers.

With a handle, then the handle id deleted, the object is manages id deleted also if it is the last handle. Otherwise, the reference count is just reduced by1.

This may require some rresearch. Check out Design Patterns ISBN 0-201-63361-2. You are interested in the Bridge pattern.

A reference counted handle will be available as a template in STL as shared_ptr in C++ 2003, which isn't quite out yet.

If you decide to go this way, let me know and I will post a template I have.
May 22 '07 #4
paulix
3
I've never used something like that, but if it works....why not? But let me explain better....what I'm reading are logfiles and each row of data contains the hour, so the first key in the map of maps is exactly the hour [0,..,23]. So I could also use an array of 24 map, i.e. instead of:

map <int, map <int, map <string, int> > > mymap;

I could use:
map <int, map <string, int> > mymap[24];

and because the logfiles are sorted text file as soon as the data concerning the first hour are finished I print the cumulative data and so I could delete the first element of the array (mymap[0]) and so on. I've tried but even in this way the memory increase with the same speed....
So of course if you could show me some pratical way to mantain the memory control usind the std map would be nice!

Many thanks!
May 22 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
I just posted an article on Handle classes. Have a look and tell me what you think.
May 23 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Richard Jones | last post by:
Garbage Collection & Memory Management Summer School 20-21 July 2004, Canterbury, UK The performance of today's memory-hungry applications depends on efficient dynamic memory management,...
2
by: DANIEL BEAULIEU J | last post by:
Basically i am a student taking an operating systems course which is c++ intensive. Familiar with Java, and so not so familiar with memory management. Looking for suggestions of exercises or web...
9
by: Mike P | last post by:
I know everything about reference counting and making sure you don't have large objects lying around. I have also profiled my app with multiple tools. I know about the fact GC collects memory but...
8
by: Chad | last post by:
hello, i am losing memory each time i make this call to a C++ dll (I make frequent calls). 'in VB.Net Declare Function grab Lib "grabber.dll" _ (ByRef lpBuf As Byte, ByVal lnum As Integer)...
1
by: trialproduct2004 | last post by:
Hi all, I am having slight confusion regarding memory management in .net. Say suppose i have two application one is in C# and other is in MFC(VC++). Both of this application are using lots...
0
by: erez_acount | last post by:
***************************************************************************** Call For Papers The 2006 International Symposium on Memory Management (ISMM'06) Co-located with PLDI 2006 ...
94
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring...
3
by: Jim Land | last post by:
Jack Slocum claims here http://www.jackslocum.com/yui/2006/10/02/3-easy-steps-to-avoid-javascript- memory-leaks/ that "almost every site you visit that uses JavaScript is leaking memory". ...
9
by: benoit808 | last post by:
I don't have a lot of experience with C++ so I apologize if this is a stupid question. I use Paul Nettle's memory manager (mmgr.cpp) which reports a memory leak but I don't think there's one. Here...
5
by: kumarmdb2 | last post by:
Hi guys, For last few days we are getting out of private memory error. We have a development environment. We tried to figure out the problem but we believe that it might be related to the OS...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.