Memory allocation of class objects

nitika tangri picture nitika tangri · Mar 20, 2013 · Viewed 17.4k times · Source

I have been asked this question in interview. Please help me find its answer.

Suppose you have a class Employee. There are 2 variables in it - 1. String Name 2. Int Age

Now, Employee emp = new Employee();

Now the Questions asked are :

  1. Where the object emp stored in memory i.e in stack or heap and how ?
  2. Where the name and age variables stored in memory and how?
  3. What does each word in this statement do i.e what does employee do..then emp..then =.. then new.. then employee.. then ()..then ;
  4. What is the difference between the above statement and Employee emp; ? Tell in terms of memory allocation.?

Please reply with your valuable comments.

Answer

Eric Lippert picture Eric Lippert · Mar 20, 2013
  1. Where the object emp stored in memory i.e in stack or heap and how ?

The question is poorly worded. emp is not an object; emp is a variable which contains a reference to an object.

So let's reword the question:

1 (a) Where is the object referred to by emp stored in memory?

The object referred to by variable emp is stored in long-term storage, also known as "the heap".

1 (b) emp is a variable and therefore represents a storage location. Where is that storage location in memory?

The question does not give enough information to say. Variable emp could be a static field, instance field, or local variable. (It cannot be a formal parameter because of the semicolon.) If a local, it could also be a closed-over outer variable of a lambda, or a local of an iterator block, or of an async method. All of these would change whether the variable's storage is in short-term or long-term storage. If it is in short-term storage it could be on the stack or it could be a register.

2 Where are the name and age variables stored in memory and how?

Since they are fields of a class, the storage locations associated with these variables are always on the long-term heap.

Since name is of type string, the thing that it refers to -- a string -- is also on the heap. (Or, the variable could be null, in which case it doesn't refer to anything.)

3 What does each word in this statement do i.e what does employee do..then emp..then =.. then new.. then employee.. then ()..then ;

The question is extremely badly worded. First off, those are not "words", those are "tokens". (And () is two tokens.) Second, it is completely unclear what the question means by "do". So let's ask a different question:

3 Describe in detail the operations performed when this declaration executes at runtime.

We cannot say with any precision because there is not enough information in the question. The question says that it is a statement, so it is not a field declaration. Let's assume for simplicity's sake that it is not in an iterator block or async method, and that the local is not an outer variable of any anonymous function.

  • First, short-term storage is allocated for the variable; it is likely to be enregistered; if not, it will be on the stack. It is assigned a null reference.

  • Second, the memory allocator is asked to produce empty memory for an instance of Employee on the heap. It does so and produces a reference to that memory.

  • Third, if this is the first time we've seen Employee and Employee has a static constructor, the static constructor runs.

  • Fourth, after the static ctor completes, Employee's field initializers run.

  • Fifth, Employee's base class constructor runs. This might cause other static constructors to execute.

  • Sixth, Employee's constructor body runs.

  • Seventh, the constructor completes and the reference to the now-initialized object is copied into its storage.

All this of course assumes that nothing along the way threw an exception.

4 What is the difference between the above statement and Employee emp; ? Tell in terms of memory allocation.

The question does not contain enough information to give an accurate answer. If the local variable is never used then the compiler is free to optimize it away. If it does not optimize it away then the storage for emp is allocated off the short-term pool, initialized to null, and never used.