• Post Reply Bookmark Topic Watch Topic
  • New Topic

Concept about Memory storage and thread  RSS feed

 
A Chavda
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
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • 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.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor 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!