answersLogoWhite

0


Best Answer

Objects are stored in an area of memory called the "heap", whilst reference variables are stored in the "stack". These are both parts of the computer's RAM memory; the Java Virtual Machine (JVM) simply reserves part of the memory available to it for the stack and for the heap, and manages them accordingly.

User Avatar

Wiki User

10y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

• Instance variables and objects live on the heap.

• Local variables live on the stack.

Let's take a look at a Java program, and how its various pieces are created and map into the stack and the heap:

1. class Maine { }

2.

3. class Lion {

4. Maine c; // instance variable

5. String name; // instance variable

6.

7. public static void main(String [] args) {

8.

9. Lion d; // local variable: d

10. d = new Lion();

11. d.go(d);

12. }

13. void go(Lion Lion) { // local variable: Lion

14. c = new Maine();

15. Lion.setName("Aiko");

16. }

17. void setName(String LionName) { // local var: LionName

18. name = LionName;

19. // do more stuff

20. }

21. }

This is how the variables and methods get placed in the stack and heap during execution of the above piece of code.

• Line 7-main() is placed on the stack.

• Line 9-reference variable d is created on the stack, but there's no Lion object yet.

• Line 10-a new Lion object is created and is assigned to the d reference variable.

• Line 11-a copy of the reference variable d is passed to the go() method.

• Line 13-the go() method is placed on the stack, with the Lion parameter as a local variable.

• Line 14-a new Maine object is created on the heap, and assigned to Lion's instance variable.

• Line 17-setName() is added to the stack, with the LionName parameter as its local variable.

• Line 18-the name instance variable now also refers to the String object.

• Notice that two different local variables refer to the same Lion object.

• Notice that one local variable and one instance variable both refer to the same String Aiko.

• After Line 19 completes, setName() completes and is removed from the stack. At this point the local variable LionName disappears too, although the String object it referred to is still on the heap.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Where do objects and references stored in memory in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What are objects in java?

Object is like a variable of the class type which references the memory required by the attributes of the class.


What is memory leak problemin java?

Memory leaks do not occur in Java as the garbage collector clears the memory which has no references.


Can a Java application have memory leak?

Java has a fairly sophisticated garbage collection system, and in general you don't worry about memory leaks, as they only happen in a few very specific circumstances (notably, when adding listeners to Swing GUI objects and not removing them). If you need more sophistcated memory management, java provides the clases in the java.lang.ref package.


Who is allocated by java memory?

Objects, mainly.


2 What is the purpose of garbage collection in java and when is it used?

Garbage collection prevents memory leaks. In Java, the Java Virtual Machine will garbage collect whenever there is memory that has no references.


What is phantom memory in java?

Phantom memory is a false memory which doesnt exist in reality .These references are available to collect byGC


How you allocate the memory in java?

In Java we need not allocate memory manually. The JVM would take care of allocating as much memory that your objects would require automatically.


What are the differences between C and Java reference variables?

Java does not have the concept of Reference Variables. We cannot access the memory location where the data is stored in Java.


Give the brief introduction of garbage collection?

Garbage collection in java is nothing but the process of freeing up unused memory from the JVM memory heap. Every time an object is created, it is allocated some space in the memory. Once the use for that object is over, that memory space needs to be released so that it can be used by other objects. In Java, this process is automated and the JVM takes care of doing this. Once in a while, a program that performs this garbage collection runs and frees up memory for other objects to use. All objects whose references are no longer used are cleared.


What is GC in java?

GC stands for garbage collector - this is the mechanism which cleans up unused object references. The GC is why memory management in Java is almost non-existent.


In Java where do instance variables stored in memory?

An instance variable is part of an object. Therefore, it gets stored together with the object, on the heap. The heap is the part of memory which is used to store objects.An instance variable is part of an object. Therefore, it gets stored together with the object, on the heap. The heap is the part of memory which is used to store objects.An instance variable is part of an object. Therefore, it gets stored together with the object, on the heap. The heap is the part of memory which is used to store objects.An instance variable is part of an object. Therefore, it gets stored together with the object, on the heap. The heap is the part of memory which is used to store objects.


How is java reliable?

Java is robust because it is highly supported language, meaning that unlike C you cannot crash your computer with a bad program. Also, another factor in its robustness is its portability across many Operating systems, with is supported by the Java Virtual Machine.