Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no heap overhead for the base reference?

From Stroustrup's Foundations of C++, he offers a pure object-oriented language (on Page 4).

class complex { double re, im; /* … */ };
complex a[ ] = { {1,2}, {3,4} };

He assumes a in the pure object-oriented language is allocated on the heap, and a's memory layout looks like: enter image description here

The likely size is 3*sizeof(reference)+3*sizeof(heap_overhead)+4*sizeof(double). Assuming a reference to be one word and the heap overhead to be two words, we get a likely size of 19 words to compare to C++’s 8 words. This memory overhead comes with a run-time overhead from allocation and indirect access to elements. That indirect access to memory typically causes problems with cache utilization and limits ROMability.

I noticed that the uppermost reference has no heap overhead (white rectangle).

I guess it is a general phenomenon rather than specified for the pure OO example language.

But I cannot find any reference (I do admit this is not a search-engine-friendly question).

Update

Thanks for your answers. However, I forgot to post my original guess. (Apologies, it is my fault.)

Actually, I also thought because a itself may be allocated on the stack or somewhere else, it will not have heap overhead itself. But later, I noted BS also says:

Compare this with a more typical layout from a “pure object-oriented language” where each user-defined object is allocated separately on the heap and accessed through a reference...

on Page 4.

So, I think that he has limited the implementation to heap-only (or stack-less).

(Of course, maybe I'm reading too much into this sentence.)

like image 738
Chen Li Avatar asked May 24 '18 05:05

Chen Li


People also ask

Why are reference types stored in heap memory?

While value types are stored generally in the stack, reference types are stored in the managed heap. A value type derives from System. ValueType and contains the data inside its own memory allocation. In other words, variables or objects or value types have their own copy of the data.

What happens if heap memory is full?

When the heap becomes full, garbage is collected. During the garbage collection objects that are no longer used are cleared, thus making space for new objects.

Is heap allocated automatically?

The heap is a memory used by programming languages to store global variables. By default, all global variable are stored in heap memory space. It supports Dynamic memory allocation. The heap is not managed automatically for you and is not as tightly managed by the CPU.

Can the heap run out of space?

In a word, yes, it can. While the heap can grow and shrink, it does have lower and upper bounds (usually defined by the -Xms and -Xmx arguments, unless you choose to se the defaults). If the heap size is exceeded, the allocation will fail and you'll get a java. lang.


2 Answers

The top-level reference lives wherever it lives. Here are a few options:

  1. It can live on the stack. a in the code could be represented like that.
  2. It is embedded into an existing object.
  3. Memory is allocated to just hold the handle. In this case it would have a memory overhead.
  4. It lives as a global entity.
  5. A reference can live in a register. a in the code could also be represented like that in which case it kind of has neither a heap overhead nor a memory overhead: instead it "only" requires the use of a register.

The main realisation in this context is that these references are not actual objects, i.e., you can't have a reference to a reference. As a result there is no need to keep the position of the reference fixed. They can be (and they are when they are actually represented) actual values. The value of a reference in a typical implementation is the address of the object.

The other entities are objects:

  • Class complex is used to create an object which actually embeds to odd double "objects" which are also values rather than actual objects.
  • a is an array object in this model referencing two complex objects.
like image 145
Dietmar Kühl Avatar answered Sep 29 '22 14:09

Dietmar Kühl


You get heap overhead when you store something on the heap. The two complex values are stored on the heap, so they get overhead. The array of references is also stored on the heap, so it gets overhead.

However, the reference to the array is not stored on the heap. Usually, this reference will be either placed on the stack as a local variable, or that stack storage might be optimized away by using a CPU register instead. In either case, the reference itself is just a local pointer variable which does not have heap allocation overhead itself.

like image 45
cmaster - reinstate monica Avatar answered Sep 29 '22 13:09

cmaster - reinstate monica