2

I have arry of MyClass

MyClass[] data;

setting length:

data = new MyClass[1];

adding data:

data[0] = new MyClass();

Now I need to clear array. What is the best way to do this? Is it ok to assign null to that array?

data=null;
BackSlash
  • 21,927
  • 22
  • 96
  • 136
vico
  • 17,051
  • 45
  • 159
  • 315
  • 1
    That depends on your application. The best way is to narrow the scope of the variable to the code really using it. – Holger Sep 13 '13 at 13:16
  • http://stackoverflow.com/questions/4208655/empty-an-array-in-java-processing – Akina91 Sep 13 '13 at 13:17
  • What do you mean by clear? Remove all elements? That's different than setting to `null`. – arshajii Sep 13 '13 at 13:17
  • Note that `data=null;` just sets the *reference* to `null`, and will make the array object itself subject to garbage collection, at which point you don't worry about it anymore. This is different than emptying the array of data but keeping the array object itself intact and referenced. – ajp15243 Sep 13 '13 at 13:19
  • 1
    Avoid usage of Array; better use ArrayList and make its reference null when its not required. – rai.skumar Sep 13 '13 at 13:20

5 Answers5

7

Do you want the array to still exist but have nothing in it? Reinitialise the array:

data = new MyClass[1];

Do you want the array to no longer exist, so that the garbage can be collected when the JVM feels like it? Then data=null; as you said.

If you have more than one reference to the same array and want to remove all elements:

Arrays.fill(data, null);
dwarduk
  • 919
  • 1
  • 5
  • 8
  • 3
    In both cases the original array will "no longer exist" and will be eligible for garbage collection, assuming it isn't referenced anywhere else. After all, you're reassigning `data` to something new in both cases. – arshajii Sep 13 '13 at 13:20
5

I dont know what you expecting.In general java have automatic garbage collection.

Arrays.fill(myArray, null);
Ami
  • 4,241
  • 6
  • 41
  • 75
1

Basically you don't need it in Java, you have automatic garbage collection.

Sometimes if you use Factories that store static data, sure, you need set it to null to prevent additional usage in the future

But if you looking for other ways you can try:

List<MyClass> data = new ArrayList<MyClass>(1);
data.add(new MyClass());
data.clear();
Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
  • I had been the original downvoter; on my first read of the question, I interpreted "clear" as simply clearing the array (length of zero). What the asker is wanting, on a second pass, is ambiguous, so I removed my vote. – Chris Forrence Sep 13 '13 at 13:19
0

Yes you can use = null and it will take care of things.

Once there is no variable pointing to the data, then garbage collector will collect it, and everything will be fine and dandy :)

Quillion
  • 6,346
  • 11
  • 60
  • 97
0

It depends, it all comes down to how the garbage collector handles it. You can do two things and the behavior and semantic is slightly different:

data[0] = null;

means that data will still keep referencing an area of memory containing a one-sized list of references to MyClass, only in this case the reference to MyClass is null. The garbage collector will collect the previously assigned instance of MyClass.

data = null;

however, will remove the mentioned area of memory altogether, and the garbage collector will collect both the list of references and the MyClass instance. So, from a memory standpoint, and if you really don't plan to use the same array of references, the second option will provide the most benefit.

Jose Cifuentes
  • 596
  • 6
  • 21