How to garbage collect arrays of objects?

I work in a huge program in java and now I try to avoid loitering in order to improve its memory usage, I create an instance of some objects in the constructor and save the instance until the end of the program, but they are not always used.My question specifically concerns garbage collectors of objects.

For example, when a user clicks on a menu item, JDialog is called with a large number of components in it, these components were created at the time the program started, but I want to create them when necessary, and release them if not.

For instance:

JRadioButton Options = new JRadioButton[20]; for (int i = 0; i < 20; i++) { Options[i] = new JRadioButton(Labels[i]); } 

If I want to free arrays, what am I doing?

It:

 for (int i = 0; i < 20; i++) { Options[i] = null; Labels[i] = null; } 

Or simply:

 Options = null; Labels = null; 

Thank you in advance

+4
source share
4 answers

First, a Java object will only collect garbage if it is inaccessible (and it may have other references than your array). Then the GC runs at an almost unpredictable time (therefore, memory can be freed much later).

Clearing the elements of an array will not release the entire array, but may free each element (provided that it becomes inaccessible).

setting the variable to null may result in the release of the array (and, of course, all elements).

But for such a small program, perhaps the GC never happens.

Read at least the GC on wikipedia and possibly the GC Handbook

Note that the property of an object is a whole program property (in fact, the whole property of the process: the significance of the values ​​is relevant in a specific implementation, and not in the source code). In other words, you could do Options = null; and still have an object in Options[24] to achieve another link path.

+4
source

If Options contains a single reference to the array, or works to make objects inaccessible and free objects in the garbage collector.

If something else still refers to the array, it still won't be released, so the first option will be the only one that will release the contents. Note that the first option will only release the contents, Options will refer to the actual Array , unless you also set the Options parameter to null.

+3
source

Performance

 Options = null; Labels = null; 

should be enough to release these objects. There is no need to nullify elements unless there is another reference to the array. However, when there are other array references, I don't think it would be wise to null the elements. There are other links for some reason. When they no longer need the array and its contents, they must release their links.

+3
source

Both will do, but first recommended, and then do the second.

Here is the source code of ArrayList clear() method

 // Let gc do its work for (int i = 0; i < size; i++) elementData[i] = null; 

Another way to do the same:

 Arrays.fill(Options, null); 

It does not perform any different iterations and sets the elements of the array to null .

+2
source

Source: https://habr.com/ru/post/1439614/


All Articles