aspose file tools*
The moose likes Java in General and the fly likes How Memory is Allocated in Java when Instantiating an Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How Memory is Allocated in Java when Instantiating an Object" Watch "How Memory is Allocated in Java when Instantiating an Object" New topic
Author

How Memory is Allocated in Java when Instantiating an Object

K Nally
Greenhorn

Joined: Sep 30, 2003
Posts: 13
Hi:
I have a basic question on when memory is allocated for an object and if that memory is "shared" between the class that instantiates it and the class itself. I am using "class" and "objects" interchangeably here.
In the following example, do we have one memory allocation of Statement st or two memory allocations (myServlet's and DBConnect)?
myServlet:
DBConnection dbconnect = new DBConnection();
Statement st = dbconnect.setConnection();
Java Class:
public class DBConnect implements Serializable {
private Connection conn = null;
private Statement st = null;
public DBConnection() {
}
public Statement setConnection() {
<other statements>
conn = ds.getConnection(...);
st = conn.createStatement();
return st;
}
}

Thanks!
[ January 30, 2004: Message edited by: K Nally ]
[ January 30, 2004: Message edited by: K Nally ]
[ January 30, 2004: Message edited by: K Nally ]
Ken Krebs
Ranch Hand

Joined: Nov 27, 2002
Posts: 451
Both references to the Statement object point to the same area of memory which is on the heap.
The memory for the "st" reference in DBConnect is allocated on the heap and is private to the DBConnect instance.
If myServlet is a "good little servlet", i.e. threadsafe, the memory for the "st" reference in DBConnect is local to the method and allocated on the stack.
If myServlet is NOT a "good little servlet", the memory for the "st" reference in DBConnect is an instance variable and is allocated on the heap.


kktec<br />SCJP, SCWCD, SCJD<br />"What we observe is not nature itself, but nature exposed to our method of questioning." - Werner Heisenberg
Greg Reinl
Ranch Hand

Joined: Feb 11, 2003
Posts: 45
Ken Krebs wrote:
If myServlet is a "good little servlet", i.e. threadsafe, the memory for the "st" reference in DBConnect is local to the method and allocated on the stack.
If myServlet is NOT a "good little servlet", the memory for the "st" reference in DBConnect is an instance variable and is allocated on the heap.

Did you mean to say that the memory for the "st" reference in the servlet is local (for the good little servlet) or an instance variable (for the bad little servlet)? If not, then I'm confused about soemthing that I thought I was just starting to understand.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12675
    
    5
Memory for actual objects is always on the heap. Calls to methods create space to store the references to objects or actual primitive values on the stack. Just keep making the distinction between the memory the object lives in and the memory the reference variable lives in and you will be ok.
Bill
 
 
subject: How Memory is Allocated in Java when Instantiating an Object
 
Similar Threads
a mistake for bean
Unable to get a Connection Object
Single Application but Multiple Connection problem
PreparedStatement's executeQuery() returning resultset with no rows
problem in jsp and database connection