473,387 Members | 1,517 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,387 software developers and data experts.

hash_map in stl c++ memory issues

2
Hi!

I have written code using STL hash_map in c++ and am running into memory issues. I have two kinds of hash_maps:
A <int,int> which stores ~5million key-value pairs, and needs to be accessed often to store in memory, its not updated during the program.
B <int,hash_map<int,float>> which is also ~5m key-value pairs, but I can read this in parts from the disk. The inner hash_map has about 20 elements.

In my program, I read in A and B1(first 100K key-values), I perform the required computation, and say B1.clear(), and read the next set of 100k elements.

1) The program exits with an std::bad_alloc error. How can I fix this?

2) What hash_fns would be good for this large data?

3) It seems that clear does not free the memory. What can I do? When I say B1.clear(), will it free the inner hash_map also?

4) How can I find out what is using up the memory?

I am a novice to stl programing, any help would be appreciated.

Thanks!
Mar 18 '08 #1
1 4439
weaknessforcats
9,208 Expert Mod 8TB
1) The program exits with an std::bad_alloc error. How can I fix this?

2) What hash_fns would be good for this large data?

3) It seems that clear does not free the memory. What can I do? When I say B1.clear(), will it free the inner hash_map also?

4) How can I find out what is using up the memory?
1) You have run out of heap memory. Check your system and see what the heap limits are. That is, your program is in a process address space and you have to stay within that range of addresses. Becuae you have a 2GB machine does not mean you cna have a 2GB heap.

2) I have no idea. You need to research here. Check out The Art of Computer Programming by Donald Knuth Volume 2 - Sorting and Searching.

3) None of the STL containers gives up memory once it it acquired. This is by design. Allocating memory is slow operation and the STL is optimized for speed so once your container gloms onto memory, it keeps it. The B1.clear() just erases all of the elements in the container but does not reduce the memory size of the container. You can force the container to give up this memory by swapping it with a container that was created empty.

4) See (3) above.
Mar 18 '08 #2

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

Similar topics

5
by: Sabrina | last post by:
Can someone help? I have been trying to get the hash_map in C++ for .NET to work with strings and const char*. I am using the const char* as the key and a pointer to another class as the data...
5
by: Charles Herman | last post by:
I have the folowing program: #include <iostream> #include <ext/hash_map> using namespace std; using namespace __gnu_cxx; typedef hash_map<char*, long, hash<char*> > char_hash_t;
2
by: gibffe | last post by:
Hello I have a big problem with hash_map in stl. I'm trying to create hash_map<const char *, char *> map to keep key => val pairs as 4 byte C "strings". Everything is ok when i do let's say...
4
by: Dymus | last post by:
problem with deleting big sized hash_map. have defined a comporator, where defined min_bucket_size (for speed improvement, since i know that i need to store big ammount of data (~1Mln)),...
3
by: kony | last post by:
Hi there, I would much appreciate your help with the following problem. Below is the code that uses a hash_map. I want to release all the memory occupied by the hash_map for other use. Apparently...
4
by: lokki | last post by:
Hello, can anybody tell me what's wrong with following example code? char *k, *v; k = new char; strcpy(k, "a2"); v = new char;
4
by: newbie | last post by:
For STL containers like hash_map, need I explicitly call clear() before delete()ing it? e.g. hash_map<string, MyClass**my_map; my_map = new (hash_map<string, MyClass*>); .... ....
6
by: SimpleCode | last post by:
class COsgCar; class moving_vechicle; hash_map<moving_vechicle*, COsgCar*m_hash; hash_map<moving_vechicle*, COsgCar*>::iterator iter; iter = m_hash.find( pMovingVechicle ); if ( iter !=...
4
by: James Kanze | last post by:
On Jul 16, 10:53 pm, Mirco Wahab <wa...@chemie.uni-halle.dewrote: It depends. You might like to have a look at my "Hashing.hh" header (in the code at kanze.james.neuf.fr/code-en.html---the...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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,...

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.