• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Memory Of the Variable

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?.


 
Ranch Hand
Posts: 326
Android Mac OS X Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.





 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic