?
Solved

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

Posted on 2014-07-14
7
Medium Priority
?
224 Views
Last Modified: 2014-07-15
Hi,
How do i find out whether a variable or object is stored on Heap or Stack in a Java Program ?

Thanks
0
Comment
Question by:Rohit Bajaj
  • 4
  • 3
7 Comments
 
LVL 14

Expert Comment

by:CPColin
ID: 40194993
All objects are stored on the heap.

Is there something specific you're doing where you need to make this distinction?
0
 

Author Comment

by:Rohit Bajaj
ID: 40195108
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
 
LVL 14

Expert Comment

by:CPColin
ID: 40195141
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Rohit Bajaj
ID: 40195160
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
 
LVL 14

Expert Comment

by:CPColin
ID: 40195166
Objects and everything inside them are stored on the heap.
0
 

Author Comment

by:Rohit Bajaj
ID: 40197531
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
 
LVL 14

Accepted Solution

by:
CPColin earned 2000 total points
ID: 40197557
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month15 days, 12 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question