Determining if a variable or Object is stored in Heap or Stack

Hi,
How do i find out whether a variable or object is stored on Heap or Stack in a Java Program ?

Thanks
Rohit BajajAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CPColinSenior Java ArchitectCommented:
All objects are stored on the heap.

Is there something specific you're doing where you need to make this distinction?
0
Rohit BajajAuthor Commented:
No i am not doing anything specific.
Wanted to experiment with simple java code and find out if something is on heap or stack.

You mean everything is stored on Heap ?

eg -
public class Test
{
	public static void main(String[] args) {
		int x = 2;
		Student s = new Student();
		Student s2 = new Student();
		s.name="robin";
		s2.name ="robin"; 
		
		
		
	}
	
	
}

class Student
{
	public String name;
	
	
}

Open in new window


In the above code are all the variable values x, s, s2 will be stored in heap ?
Also where are the references themselves stored ? like x,s,s2?

Is it documented somewhere what is stored in heap and what on stack ?

Also how does one find the address of an object or variable in Java ?

Thanks
0
CPColinSenior Java ArchitectCommented:
In the above code are all the variable values x, s, s2 will be stored in heap ? Also where are the references themselves stored ? like x,s,s2?

The x variable is a primitive, so it is stored on the stack. The s and s2 variables are references to objects, so they're also stored on the stack. The Student objects themselves are stored on the heap.

Is it documented somewhere what is stored in heap and what on stack ?

The Java Virtual Machine Specification has some info on the subject: http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html#jvms-2.5

Also how does one find the address of an object or variable in Java ?

In general, you don't, but it's rare that you have to. Typically, though, the default implementation of Object.hashCode() returns the address of the object, converted to an integer.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Rohit BajajAuthor Commented:
Hi,
The Student object is stored on Heap .
But suppose if my class is like :
class Student
{
String s;
int i;
}

Then in this case is i do Student st = new Student();
Where will the following get stored -
1) s
2) st
3) i

Including there value and the reference ?
0
CPColinSenior Java ArchitectCommented:
Objects and everything inside them are stored on the heap.
0
Rohit BajajAuthor Commented:
Thanks that helped a lot.
One more thing is left.
in a function call say add(int x, int y)
where will the values of x and y and there references stored ?

Is there any way to programmatically find out if something will be stored on Stack or Heap.
Also i read something on the page you shared :
Because the Java Virtual Machine stack is never manipulated directly except to push and pop frames, frames may be heap allocated.

What does this exactly means ?
0
CPColinSenior Java ArchitectCommented:
in a function call say add(int x, int y)
where will the values of x and y and there references stored ?

Traditionally, most would consider those values to be part of the "stack frame" of the method being called, but as you go on to quote from the spec, "frames may be heap allocated." I did not know that before now. I guess, as a way of optimizing memory, the JVM is not required to implement frames as a stack. Ultimately, it doesn't matter, because of my answer to your next question:

Is there any way to programmatically find out if something will be stored on Stack or Heap.

Nope. And, per that bit you quoted from the spec, it sounds like the JVM doesn't really care all that much. In general, though, you can still think of local variables and method parameters as being on the stack and objects as being on the heap.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.