File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Concept about Memory storage and thread Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Concept about Memory storage and thread" Watch "Concept about Memory storage and thread" New topic
Author

Concept about Memory storage and thread

A Chavda
Greenhorn

Joined: Apr 15, 2008
Posts: 3
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
Marshal

Joined: Jul 08, 2003
Posts: 24057
    
  13

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.


[Jess in Action][AskingGoodQuestions]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16695
    
  19

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Concept about Memory storage and thread
 
Similar Threads
What is stored on Stack and Heap
post increment operator
java compiler issues
About object ,reference and class variable
Stati variables