• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stack and Heap  RSS feed

 
Mandar Puranik
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Many of the topics here refer to stack and heap.
Can anybody explain these in detail please ?
(I only know that these are different type of memories).
Is it that we can create objects or variables on stack as well as heap ? Whats the difference ?
An example of each would be very nice.
Thanks a lot Ranchers
Mandar
 
Jon Strayer
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

One variable is declared (o). This varaible is stored on the stack. When the method returns, the stack memory is automatically reclaimed.
The variable o refers to an unnamed Object that is created in the heap (via the keyword 'new'). When there are no more references to this Object the garbage collector is free to reclaim the space.
I'm sure this doesn't completely answer your question, but ask some more and I'll try again.
 
Mandar Puranik
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jon,
Got your point. Now can you explain what you mean by stack ? Can we create an object or variable on stack ? An example would be more useful.
Thanks
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mandar LearningJava,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mandar LearningJava:
Now can you explain what you mean by stack ?

Every Method in Java is associated to a "Frame" (not an AWT frame). Each Frame has it's own stack to store variables and partial computations. If scope is passed to a different Frame the original Frame is still there but the new Frame becomes "current". When the method is over, the Frame (and therefore it's stack) is gone and cleaned up by the JVM. (Just to add confusion, it is possible that SOME JVMs may implement stacks on the heap . But logically you can ignore this.)
From the Java Virtual Machine Specifications:
3.5.2 Java Virtual Machine Stacks
Objects are created on the heap. This is an area of memory reserved for such stuff. Of course in each object there are variables that are not related to methods, and are therefore given the special name of fields.
It is important to take care not to confuse the concept of a variable, with the object that it references.
3.5.3 Heap

Just for fun:
Not all Variables are created Equal
 
Jon Strayer
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mandar LearningJava:
Thanks Jon,
Got your point. Now can you explain what you mean by stack ?

It's just a segment of memory that's used in a particular way.


Can we create an object or variable on stack ? An example would be more useful.
Thanks

Objects are always stored in the heap (which is just another segment of memory). Local variables (including object references) are stored on the stack.
You don't have to do anything special to use the stack or the heap. This is all just part of the language/vm spec.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!