Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is better in terms of memory allocation - subobject or a pointer to a separate object?

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:

  1. If I store subobjects inside traversing to them will become slightly faster, but that's not what I care about the most - I profiled the program and that's not the bottleneck.
  2. If I store subobjects inside the outer object will occupy more memory, so I'll allocate larger blocks and that might fragment memory more compared to allocating lots of similarly small subobjects separately - freed memory blocks might be less reusable (I'm not sure, I just suppose that).
  3. Of course, less allocations will make the program faster, but that's not very important - again I profiled the program and allocations time is not a bottleneck.
  4. Since each allocation induces a memory overhead with larger objects I will in fact consume less memory and that is good for my program that works with huge amounts of data.

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?

like image 793
sharptooth Avatar asked Oct 15 '22 02:10

sharptooth


1 Answers

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.

like image 136
Jerry Coffin Avatar answered Oct 18 '22 15:10

Jerry Coffin