I have the following problem in a Visual C++ 9 program. There's a huge object that logically contains several subobjects. I can either store the subobjects inside the object or store pointers to subobjects allocated separately.
The key point here is that there's always one instance of suboject of each type in one outer object - it is always of the same size and its lifetime is exactly the same as of the outer object, so the two choices above are logically identical - the program structure and logic don't change.
Here's how my thought develops:
Meanwhile memory fragmentation is what really bothers me - I need my program to be very stable and able to run from months continuously.
How do I make my decision? Are there any methods for deciding whether I should choose smaller object or bigger objects given the considerations above?
Allocating a few large objects will fragment memory less than allocating a larger number of smaller objects. Even more important, since a larger percentage of your objects are exactly the same size, you'll often be able to reuse a freed block exactly as-is. Splitting your object up into a larger number of smaller objects will generally make fragmentation more of a problem, not less.
If you find that fragmentation does become a problem, you generally want to deal with that by defining your own allocator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With