thausands objects of a class

Jan 15, 2009 at 8:03pm
Hi All,

I have a c++ program that creates thousands of objects.
I need all of this objects at run time ,but memory is
limiting me.
For example, 1,000,000 objects of a class is needed.
How could I handle this problem ?
Jan 15, 2009 at 9:21pm
Use smarter ways to handle the information. How to this would depend on the situation.
Jan 15, 2009 at 9:21pm
Jan 15, 2009 at 10:00pm
I see only three solutions:
1. Get more memory. This seems like the fastest and more straightforward.
2. Reduce the size of the objects. One way could be searching duplicate data and having the duplicates point to, rather than have copies of it. This should be handled carefully.
3. Keep the unnecessary data on disk. You could have a class called objectsCache that will handle serialization (writing to disk) and deserialization (reading from disk) and give it a limit of objects to keep in memory. The most important parameters a cache needs are its size, and which elements to remove (e.g. at random, the oldest, the least accessed, etc.).
Jan 15, 2009 at 10:40pm
One million objects is running you out of memory? Those would have to be pretty large objects; are you trying to put them on the stack?
Jan 15, 2009 at 10:45pm
There is no point answering this question, as the OP as failed to give anywhere enough information. The OP didn't even tell us what object was being stored, how, where etc. Completely useless post IMO.
Jan 15, 2009 at 10:58pm
Agreed. But just for kicks, I created a class that has a 1 KB array inside it and dynamically created objects and stored them in a list until it core dumped running from Cygwin on a Windows XP machine with 2 GB of memory.

It created 876,697 objects before it failed. That's 897,737,728 bytes!
Jan 16, 2009 at 5:32am
I am using Borland C++ , RAM : 512 ,Windows XP
and my program is a simulation program which
creates objects with poisson distribution in each second
and then some operation on them.
as the simulation time goes bigger the number of objects increases.
Now how could I Keep the unnecessary data on disk? do you have a sample of code about working with files ?

"You could have a class called objectsCache that will handle serialization (writing to disk) and deserialization (reading from disk) and give it a limit of objects to keep in memory"

Jan 16, 2009 at 3:24pm
Jan 19, 2009 at 7:20pm
IMO. Better to store it in a database than on disk in a file. It'll be faster to load it.
Topic archived. No new replies allowed.