0 votes
in Programming by
Hi all,

Can anyone tell me what is stored on a stack and what is stored on a heap in a Java program?

Thanks.
related to an answer for: What is heap and stack?
   

3 Answers

0 votes
by Hot Users (380 points)


AS I Think
Primitives are stored on the stack, objects in the heap space.

int j = 20; will be stored on the stack, because the variable itself holds the value and it's much faster, but if you wrap it:
Integer p = new Integer(20); then it's stored in the heap space because it's now an object.

by Hot Users (2.3k points)
@support Well Heap and stack are the two main memories that JVM is concerned as far as memory allocation by the OS(operating system) is concerned. As said above, stack and heap are the memories allocated by the OS to the JVM that runs in the system. Stack is a memory place where the methods and the local variables are stored. Heap is a memory place where the objects and its instance variable are stored. Also it is to be remembered that the variable references (either primitive or object references) are stored in the stack. i have written this to the best of knowledge. Any corrections are welcome. Thank You.
by Hot Users (380 points)
@sarada "Stack is a memory place where the methods and the local variables are stored." By "methods", I am assumming that you mean the callstack -- not the actual code for the methods. "Heap is a memory place where the objects and its instance variable are stored." Yes. "Also it is to be remembered that the variable references (either primitive or object references) are stored in the stack." Only for local variables. For instance and class variables, they are part of the object, hence, on the heap. Finally, Java 6 may throw a monkey wrench into this. Due to "escape analysis" optimization, sometimes Java 6 will allocate objects on the stack. Thanks.
by Hot Users (2.3k points)
@support Thanks. But can anyone say where Interface variables are stored ? heap or stack?
by Hot Users (380 points)
@sarada Heap, since interfaces only exists as implementations in classes and they are on the heap.
by Hot Users (380 points)
For better understanding i am providing the below example. Remember in Runtime Environment Data, we have another field named "Method Area" which contains Method information and class variables as well In my opinion, the separation between stack and heap can be deemed as following: Stack: local variables (both primitive
0 votes
by Hot Users (380 points)
  • Instance variables and Objects lie on Heap. Remember this is where the state is maintained and when you get memory leaks this is where your profiler helps you to find the allocation of memory.
  • Local variables and methods lie on the Stack. So if we have a main method which calls the go() method which calls the gone() method then the stack from top to bottom would consist of

gone()

go()

main()

as soon as gone() has been processed it would be removed from the stack. Any corresponding local variables which are used in gone() would also be removed from the stack. Stack would have references to objects on the Heap.

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 Collar { }
2.
3. class Dog {
4. Collar c; // instance variable
5. String name; // instance variable
6.
7. public static void main(String [] args) {
8.
9. Dog d; // local variable: d
10. d = new Dog();
11. d.go( d ) ;
12. }
13. void go(Dog dog) { // local variable: dog
14. c = new Collar();
15. dog.setName(“Aiko”);
16. }
17. void setName(String dogName) { // local var: dogName
18. name = dogName;
19. // do more stuff
20. }
21. }

StacknHeap.png

 

 

 

 

 

 

 

■ Line 7—main() is placed on the stack.
■ Line 9—reference variable d is created on the stack, but there’s no Dog object yet.
■ Line 10—a new Dog 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 dog parameter as a local variable.
■ Line 14—a new Collar object is created on the heap, and assigned to Dog’s instance variable.
■ Line 17—setName() is added to the stack, with the dogName 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 Dog 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 dogName disappears too, although the String object it referred to is still on the heap.

0 votes
by
Stack

When a function or a method calls another function which in turns calls another function etc., the execution of all those functions remains suspended until the very last function returns its value.

This chain of suspended function calls is the stack, because elements in the stack (function calls) depend on each other.

The stack is important to consider in exception handling and thread executions.

Heap

The heap is simply the memory used by programs to store variables. Element of the heap (variables) have no dependencies with each other and can always be accessed randomly at any time.

Related questions

0 votes
2 answers 94 views
0 votes
3 answers 167 views
asked Nov 6, 2011 in Computers by sarada Hot Users (2.3k points)
0 votes
1 answer 84 views
0 votes
0 answers 71 views
0 votes
5 answers 578 views
Welcome to Find4Answers.com

Where you can Ask Questions, Find Answers Or Receive Answers from other members of the community And Share in Social networking sites like facebook, linkedin, twitter.
3cx phone system bandar bola benches boiler business bóg chwilówka przez internet car car lease deals car leasing uk chwilówka przez internet zapewne company convey convey law complaints convey law reviews convey law service conveylaw cornwall cosmetic dentist data data recovery maidenhead data recovery nottingham deals deepika padukone deepika padukone hot design development dot net programming dzieje szybkie chwilówki electrician electrician manchester electricians emergency farm filmy bez limitu filmy bez limitów filmy online finlock finlock solutions general genral graduate jobs in london graduate jobs london gutters hard drive recovery heap http: www.seo-assist.in infrared sauna infrared sauna saunas inline skating installation jabong coupon java judi bola laptop recycling law law firm in leeds lease leasing led lights led bulbs leeds life insurance quotes log london manchester movies museums and art myntra coupon nikogo szybka chwilówka outsource link building personal porcelain veneers ramię szybkie chwilówki recovery seo seo agencies seo company seo company london seo services services solutions stair lifts steel synchronization szybka chwilówka przykład szybka chwilówka żaden szybkie chwilówki lekko thread tunnel lighting tymczasem chwilówki przez internet upvc windows manchester viagra kamagra videos vinyl flooring suppliers wait web design services london web development company window repairs london windows yebhi coupon zobaczyć szybka chwilówka
...