Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Concept about Memory storage and thread

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have to clear my concept and so please let me know if I am wrong anywhere.

Memory storage - We have stack and heap. Object and instance variables are stored in heap and local variables are stored in stack. also the refrences to the objects are stored in stack.

Thread concept - Say we have method and we have local variable x decalred inside the method. Thread 1 invoke the method and sets the value of variable x as 10 and thread2 invoke the method and sets the variable x as 12.

Question: Will we have two stack for two threads having variable x in both with value 10 and 12 correspondingly.

I mean one stack will have x=10 and other stack will have x=12

Answer for this will help me a lot in understanding the concept deeper.

Thanks in advance
A chavda
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, each thread has its own completely different stack. Every time a method is called -- on the same thread, or on different threads -- space is reserved for a new set of local variables which is completely independent of any other set.
 
author
Posts: 23835
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Memory storage - We have stack and heap.



Yes.

Object and instance variables are stored in heap and local variables are stored in stack.



Well... yeah. But instance variables are part of objects, so it is sufficient to say "Objects are stored in heap".

Now... unfortunately, there is a monkey wrench. Java 6 changes this somewhat with "escape analysis" -- sometimes objects are on the stack.

also the refrences to the objects are stored in stack.



Well, a reference can be a instance variable, or it can be a local variable. Doesn't this statement partially conflict with your previous statement?

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!