0

I am trying to create my own version of the ArrayList. I am implementing it in such a way that when you run my add method, it will create a new array of generic objects. Then it will copy each element to the new larger array and in the last element would be the new item they added.

public void add(E data)
{
   if(this.arr == null)
   {
       this.arr = (E[]) new Object[size];
   }
   else
   {
       this.size++;
       Object[] temp = (E[]) new Object[arr.length+1];
       for(int i = 0; i < arr.length; i++)
           temp[i] = arr[i];
       temp[arr.length] = data;
       this.arr = temp;
   }
}

My code may not be correct since I did it really fast without testing it, not all of that should effect my question.

I know that in Java each object (in my case I am testing Integer objects) references a spot in memory. What I am not sure of is how the array works when copying objects in this part of my code:

for(int i = 0; i < arr.length; i++)
    temp[i] = arr[i];

Each temp[i] spot is just referencing what arr[i] references and does not create a new spot in memory. Is this statement correct?

If I add 100 items, this means this method will run 100 times. If each element in the current array is just copied into new array of objects bigger by one, does that mean that none of the previous arrays will get deleted by the garbage collector since the new elements still point to the same memory spots?

If I create an array of size 1, it creates a block of memory that holds that array. If I then create a another array of size 2, the first element is copied to it and points to it, I then reassign my variable to point to that new array (this.arr = temp)/

Arr 1: [0]
Arr 2: [0][1] <- [0] points to the same object as [0] from arr 1
Arr 3: [0][1][2] <- [0] points to the same object as [0] from arr 1, [1] points to the same object as [1] from arr 1

Will the garbage collector not delete the space used by Arr 1, when one or more of its elements are pointed too by Arr 2? And the same for Arr 3? Do I need to re allocate every single element into a new array each time it grows?

7
  • 1
    The space used by an array is just the space required for the references and doesn't include the element objects themselves. So the intermediate arrays will all be collected, but the elements themselves won't, since there's always a new array pointing to them. Commented Jul 9, 2015 at 9:19
  • Use the Object array to do all the internal working. while retrieving an Object at that time cast into generic object. i.e. (E)arr[i]. Commented Jul 9, 2015 at 9:51
  • Why are you casting this Object[] temp = (E[]) new Object[arr.length+1]; ...No Casting required in this line . Commented Jul 9, 2015 at 9:54
  • @biziclop : so the elements the elements within arr take up memory, but not the obj arr itself does not? And sorry by asking this but I want to be sure, by intermediate arrays, your talking about every arr object that eventually gets replaced by a bigger array will be garbaged collected? Commented Jul 9, 2015 at 20:00
  • @Rahul Thachilath : so dont cast it to (E[]) until I return it from the class or anything that has to do with the outside of the class? Commented Jul 9, 2015 at 20:01

2 Answers 2

1

Each temp[i] spot is just referencing what arr[i] references and does not create a new spot in memory. Is this statement correct?

Correct; you are dealing with object references you are not creating new objects. When you use temp[i] you are dealing with an object reference (a pointer in c/c++); you are not copying objects but object references. Just to clarify a little more

temp[i] = arr[i];

copies object reference; the referenced object is the same

temp[i] = new E(arr[i]);

allocates a new Object on temp[i] using another one (for the sample I'm assuming that E has a copy constructor, i.e. a contructor that takes an object of the same class as parameter); in this case you end up with two different objects.

Will the garbage collector not delete the space used by Arr 1, when one or more of its elements are pointed too by Arr 2? And the same for Arr 3?

yes it will garbage collected; you must distinguish array from its elements. If an array is not referenced anymore it will be garbage collected; in your case since the array is not accessible by your program so its memory can be released. The objects referenced by the array follow the same rule; if they are not accessible by your program the will be garbage collected, otherwise no. In your code array elements will not garbage collected since they are referenced by arr array of your class. So Arr 1 and Arr 2 in your pseudo code became eligible for garbage collection just after

 this.arr = temp;

because the "old/previous" reference of arr is not accessible by your program.

Sign up to request clarification or add additional context in comments.

4 Comments

Great explanation :) I have a few questions to continue from this <br/> 1) i am guessing if i ran this, temp[i] = arr[i]; and skipped an element, it will be deleted by GC? <br /> 2) E does not have a copy constructor, I am actually not sure If I should make one so this brings up my next question, in an ArrayList, what is the proper way to do it? Is the way I am doing it on, or do I need to do a copy constructor and reallocate each time??
1) yes if you skip an element when copying it will be garbage collected if your program has no more references to it (i.e. you used the object in your code and you stored a reference elsewhere) 2) The copy constructor was only an example for showing the difference between object and object references, you are doing it right, usually there is no need to clone array objects.
@TannerSummers I suggest you to change the title of your question to How garbage collector works with array copying
ya I guess I should, but I got my answer so I am good for right now, thank you
1

[...] each temp[i] spot, is just pointing/referencing what arr[i] references and does not create a new spot in memory. Is this statement correct?

Correct.

[...] none of the previous arrays (by that i mean every array allocated before I do the this.arr = temp) will get deleted by garbage collector since the elements still point to the same memory spit, but just another array points to it?

Correct. The pointers of the arrays are deleted. the memory space that was pointed to is not deleted until all pointers are removed. You do not have to reallocate memory when assigning array items.

Note: This is true only for Objects. arrays of primitives behave differently.

3 Comments

Ok this brings up a few more questions. SO what you are saying is the memory allocated (the elements) will still remain pointed to by each new array created, and each pointer to each array will be deleted (the arr after each arr = temp;) ok that make sense, thank you :). Now I want to ask if you wanted to delete the entire array (includes the elements), you need to manually set each element = null AND set the variable to null (arr = null)? so if I wanted to make a clear method, I loop the array setting each element to null then the object arr itself?
please take a second look at the answers I provided. it states: "The pointers of the arrays are deleted". it means you do not have to go over ans set them to bull. the Object in heap (what you call memory space) is deleted after there is no pointers that point to it. note: comments are not meant for further questioning
would be nice if you accpeted the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.