Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory allocation of class objects [closed]

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.

like image 836
nitika tangri Avatar asked Mar 20 '13 10:03

nitika tangri


People also ask

How memory is allocated to objects of a class?

In Java, when we only declare a variable of a class type, only a reference is created (memory is not allocated for the object). To allocate memory to an object, we must use new(). So the object is always allocated memory on the heap (See this for more details).

What is memory allocation of objects?

Memory allocation is the process of setting aside sections of memory in a program to be used to store variables, and instances of structures and classes. There are two basic types of memory allocation: When you declare a variable or an instance of a structure or class.

How memory is allocated to an object of a class with example?

The memory is only allocated to the variables of the class when the object is created. The memory is not allocated to the variables when the class is declared. At the same time, single variables can have different values for different objects, so every object has an individual copy of all the variables of the class.

At what time memory is allocated to objects?

1 Answer. For explanation: The object memory allocation takes place when the object constructor is called. Declaration of an object doesn't mean that memory is allocated for its members. If object is initialized with another object, it may just get a reference to the previously created object.


2 Answers

  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.

like image 130
Eric Lippert Avatar answered Sep 28 '22 00:09

Eric Lippert


You should see: The Stack Is An Implementation Detail, Part One and Part Two By Eric Lippert

1 - Where the object emp stored in memory i.e in stack or heap and how ?

On heap, because its a reference type since Employee is a class.

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

They are also stored on heap. Although age is a value type, but value types are stored where their container reference is stored.

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

Create a new instance of Employee class named emp

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

Employee emp; means just declaration, not instantiation. That means no memory is allocated to the object and it will hold null.

like image 34
Habib Avatar answered Sep 28 '22 01:09

Habib