aspose file tools*
The moose likes Java in General and the fly likes Memory Of the Variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Memory Of the Variable" Watch "Memory Of the Variable" New topic
Author

Memory Of the Variable

Ramesh Sunkara
Ranch Hand

Joined: Mar 01, 2010
Posts: 78
Hi Folks,
I have very basic doubt. Please do clarify me.



1) In the method getEmployeeDetails() since am creating an new java.lang.ArrayList it allocates/reserves memory for default capacity i.e., for 10 java.lang.String objects.
( this is my understanding,please let me know if am wrong)

2)So, when that method returns the empDetailsList since empDetailsList is method level variable,it will be eligible for GC.


3) Now, does
empDet
will also consume the same amount of memory since we assigned the value( since Java passes by value) which we got from the method getEmployeeDetails() to it ?.


Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

Ramesh Sunkara wrote:<cut>

1) In the method getEmployeeDetails() since am creating an new java.lang.ArrayList it allocates/reserves memory for default capacity i.e., for 10 java.lang.String objects.
( this is my understanding,please let me know if am wrong)

2)So, when that method returns the empDetailsList since empDetailsList is method level variable,it will be eligible for GC.


3) Now, does
empDet
will also consume the same amount of memory since we assigned the value( since Java passes by value) which we got from the method getEmployeeDetails() to it ?.




1. You are wrong... ;) The array list in itself are backed by an array of what type you are allocating. But it does NOT allocate memory enough to fit any String (or object) into it, it allocates memory enough for references to String instances (or objects).

A small "memory run" as an example:

Command/Memory allocated
al = new ArrayList<String>()/alloc(10 x 2 bytes)
al.add("Lorem ipsum")/alloc(12 x 2 bytes) put the ref to that memory into the array.


It is impossible to know how much memory the Strings (or objects) will take when you create the Array.

2. Yes, the memory that holds the reference to empDetailsList removed since it belongs to the method stack/context.

3. No. You are not returning a COPY of the ArrayList, you are returning the value of the reference. If bake a Tau (2 pie) and give them to you, they are still taking up the same space, but you have to hold them in your hands and my reference to them are no more.
Ramesh Sunkara
Ranch Hand

Joined: Mar 01, 2010
Posts: 78
The array list in itself are backed by an array of what type you are allocating.

1) I didn't understand what is backed by an array. Can you please clarify me?


al.add("Lorem ipsum")/alloc(12 x 2 bytes) put the ref to that memory into the array.


2) Why it will allocate 12 x 2 when there are only 11 characters (including space) ?

It is impossible to know how much memory the Strings (or objects) will take when you create the Array.

3) thanks a lot. I never knew this.


4) value of the reference --> is the value of reference means the address of the memory space?

5)
If bake a Tau (2 pie) and give them to you, they are still taking up the same space, but you have to hold them in your hands and my reference to them are no more.

i didnt understand this example. what exactly you meant?

6) will this return the reference i.e., address of the arraylist.
In that case will just point to that same address?

Thanks a lot for your valuable time.





 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Memory Of the Variable