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 ?
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.).
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.
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!
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"