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;
5
  • 1
    That depends on your application. The best way is to narrow the scope of the variable to the code really using it. Commented Sep 13, 2013 at 13:16
  • stackoverflow.com/questions/4208655/… Commented Sep 13, 2013 at 13:17
  • What do you mean by clear? Remove all elements? That's different than setting to null. Commented Sep 13, 2013 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. Commented Sep 13, 2013 at 13:19
  • 1
    Avoid usage of Array; better use ArrayList and make its reference null when its not required. Commented Sep 13, 2013 at 13:20

5 Answers 5

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);
Sign up to request clarification or add additional context in comments.

1 Comment

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.
5

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

Arrays.fill(myArray, null);

Comments

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();

1 Comment

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.
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 :)

Comments

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.

Comments

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.