How to delete an array properly in Java [duplicate] The 2019 Stack Overflow Developer Survey Results Are InDeleting an object in java?List of objects that are still referenced after gc()Is Java “pass-by-reference” or “pass-by-value”?Create ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?PHP: Delete an element from an arrayHow do I determine whether an array contains a particular value in Java?How do I declare and initialize an array in Java?How do I remove a particular element from an array in JavaScript?How to use foreach with array in JavaScript?Why is it faster to process a sorted array than an unsorted array?

I am an eight letter word. What am I?

Is bread bad for ducks?

APIPA and LAN Broadcast Domain

Why are there uneven bright areas in this photo of black hole?

Is it safe to harvest rainwater that fell on solar panels?

What could be the right powersource for 15 seconds lifespan disposable giant chainsaw?

If a sorcerer casts the Banishment spell on a PC while in Avernus, does the PC return to their home plane?

How to support a colleague who finds meetings extremely tiring?

Deal with toxic manager when you can't quit

Can a flute soloist sit?

Why didn't the Event Horizon Telescope team mention Sagittarius A*?

Why isn't the black hole white?

Why does the nucleus not repel itself?

What's the name of these plastic connectors

How do I free up internal storage if I don't have any apps downloaded?

Relationship between Gromov-Witten and Taubes' Gromov invariant

A word that means fill it to the required quantity

Worn-tile Scrabble

Is it possible for absolutely everyone to attain enlightenment?

For what reasons would an animal species NOT cross a *horizontal* land bridge?

Why “相同意思的词” is called “同义词” instead of "同意词"?

The phrase "to the numbers born"?

Match Roman Numerals

How to translate "being like"?



How to delete an array properly in Java [duplicate]



The 2019 Stack Overflow Developer Survey Results Are InDeleting an object in java?List of objects that are still referenced after gc()Is Java “pass-by-reference” or “pass-by-value”?Create ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?PHP: Delete an element from an arrayHow do I determine whether an array contains a particular value in Java?How do I declare and initialize an array in Java?How do I remove a particular element from an array in JavaScript?How to use foreach with array in JavaScript?Why is it faster to process a sorted array than an unsorted array?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








12
















This question already has an answer here:



  • Deleting an object in java?

    7 answers



I'm 4 days old in Java and from the tutorials I've searched, the instructors focus a lot of effort in explaining how to allocate a two dimensional array (e.g.) as such:



Foo[][] fooArray = new Foo[2][3];


... but I've not found any that explains how to delete them.



From what is going on memory-wise, the variable fooArray will point to a block of memory in the heap, in which there are 2 elements. Each of the elements points to another block in the heap as well, which have 3 elements.



That being said, could I just reference the first block of elements and the garbage collector will do the job?



Foo[1] = null; and Foo[2] = null;



Or do I have to null each of the instantiated Foo elements?



Foo[1][1] = null; Foo[1][2] = null; Foo[1][3] = null; ...










share|improve this question















marked as duplicate by TT., Caleth, Mormegil, Iłya Bursov, Andrey Tyukin Apr 8 at 16:25


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 1





    @TT. although the answer is the same, my question was specific in arrays. Meaning that, even if I've read it before I asking, I would still be in doubt (from a nooby perspective)

    – Chronus
    Apr 8 at 7:38







  • 4





    Ok I hear you. Know that everything apart from primitive data types (e.g. int, double, ...) are objects. Important to know.

    – TT.
    Apr 8 at 7:49


















12
















This question already has an answer here:



  • Deleting an object in java?

    7 answers



I'm 4 days old in Java and from the tutorials I've searched, the instructors focus a lot of effort in explaining how to allocate a two dimensional array (e.g.) as such:



Foo[][] fooArray = new Foo[2][3];


... but I've not found any that explains how to delete them.



From what is going on memory-wise, the variable fooArray will point to a block of memory in the heap, in which there are 2 elements. Each of the elements points to another block in the heap as well, which have 3 elements.



That being said, could I just reference the first block of elements and the garbage collector will do the job?



Foo[1] = null; and Foo[2] = null;



Or do I have to null each of the instantiated Foo elements?



Foo[1][1] = null; Foo[1][2] = null; Foo[1][3] = null; ...










share|improve this question















marked as duplicate by TT., Caleth, Mormegil, Iłya Bursov, Andrey Tyukin Apr 8 at 16:25


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 1





    @TT. although the answer is the same, my question was specific in arrays. Meaning that, even if I've read it before I asking, I would still be in doubt (from a nooby perspective)

    – Chronus
    Apr 8 at 7:38







  • 4





    Ok I hear you. Know that everything apart from primitive data types (e.g. int, double, ...) are objects. Important to know.

    – TT.
    Apr 8 at 7:49














12












12








12


3







This question already has an answer here:



  • Deleting an object in java?

    7 answers



I'm 4 days old in Java and from the tutorials I've searched, the instructors focus a lot of effort in explaining how to allocate a two dimensional array (e.g.) as such:



Foo[][] fooArray = new Foo[2][3];


... but I've not found any that explains how to delete them.



From what is going on memory-wise, the variable fooArray will point to a block of memory in the heap, in which there are 2 elements. Each of the elements points to another block in the heap as well, which have 3 elements.



That being said, could I just reference the first block of elements and the garbage collector will do the job?



Foo[1] = null; and Foo[2] = null;



Or do I have to null each of the instantiated Foo elements?



Foo[1][1] = null; Foo[1][2] = null; Foo[1][3] = null; ...










share|improve this question

















This question already has an answer here:



  • Deleting an object in java?

    7 answers



I'm 4 days old in Java and from the tutorials I've searched, the instructors focus a lot of effort in explaining how to allocate a two dimensional array (e.g.) as such:



Foo[][] fooArray = new Foo[2][3];


... but I've not found any that explains how to delete them.



From what is going on memory-wise, the variable fooArray will point to a block of memory in the heap, in which there are 2 elements. Each of the elements points to another block in the heap as well, which have 3 elements.



That being said, could I just reference the first block of elements and the garbage collector will do the job?



Foo[1] = null; and Foo[2] = null;



Or do I have to null each of the instantiated Foo elements?



Foo[1][1] = null; Foo[1][2] = null; Foo[1][3] = null; ...





This question already has an answer here:



  • Deleting an object in java?

    7 answers







java arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 8 at 7:18









Jason

9,69733545




9,69733545










asked Apr 8 at 7:07









ChronusChronus

1147




1147




marked as duplicate by TT., Caleth, Mormegil, Iłya Bursov, Andrey Tyukin Apr 8 at 16:25


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by TT., Caleth, Mormegil, Iłya Bursov, Andrey Tyukin Apr 8 at 16:25


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 1





    @TT. although the answer is the same, my question was specific in arrays. Meaning that, even if I've read it before I asking, I would still be in doubt (from a nooby perspective)

    – Chronus
    Apr 8 at 7:38







  • 4





    Ok I hear you. Know that everything apart from primitive data types (e.g. int, double, ...) are objects. Important to know.

    – TT.
    Apr 8 at 7:49













  • 1





    @TT. although the answer is the same, my question was specific in arrays. Meaning that, even if I've read it before I asking, I would still be in doubt (from a nooby perspective)

    – Chronus
    Apr 8 at 7:38







  • 4





    Ok I hear you. Know that everything apart from primitive data types (e.g. int, double, ...) are objects. Important to know.

    – TT.
    Apr 8 at 7:49








1




1





@TT. although the answer is the same, my question was specific in arrays. Meaning that, even if I've read it before I asking, I would still be in doubt (from a nooby perspective)

– Chronus
Apr 8 at 7:38






@TT. although the answer is the same, my question was specific in arrays. Meaning that, even if I've read it before I asking, I would still be in doubt (from a nooby perspective)

– Chronus
Apr 8 at 7:38





4




4





Ok I hear you. Know that everything apart from primitive data types (e.g. int, double, ...) are objects. Important to know.

– TT.
Apr 8 at 7:49






Ok I hear you. Know that everything apart from primitive data types (e.g. int, double, ...) are objects. Important to know.

– TT.
Apr 8 at 7:49













3 Answers
3






active

oldest

votes


















15














Explanation



You can not explicitly delete something in Java. It is the garbage collectors job to do that. It will delete anything which is not used anymore by anyone. So either



  1. let the variable fall out of scope or

  2. assign null

  3. or any other instance to it.

Then the array instance (as well as its subarrays) is not referenced anymore and the garbage collector will delete it eventually.




References



To understand why re-assigning the outer array is enough to also delete the inner arrays, you need to understand how they are referenced. Again, the garbage collector will delete anything which is not referenced. So let's take a look at an array such as:



int[][] outer = 1, 2, 3, 4, 5, 6;


We have 4 array instances. One is of type int[][] and three of type int[]. Also, we have one variable outer. The instances are referenced as follows:



 ___> 1, 2
|
outer --> int[][] ---|---> 3, 4
|
|___> 5, 6


So by deleting outer, nobody references int[][] anymore. The garbage collector can now delete it. But that also removes all references to the inner arrays, so the garbage collector can now also delete them.



Now assume that you would reference one of the inner arrays by another variable:



int[][] outer = 1, 2, 3, 4, 5, 6;
int[] thirdInner = outer[2];
other = null; // remove the reference


The situation is now



outer --> null

___> 1, 2
|
int[][] ---|---> 3, 4
|
|______> 5, 6
|
thirdInner _______________|


So the garbage collector will now delete the outer array int[][], which also removes all references to the first and second inner array. But the third is still referenced by thirdInner, so after garbage collection we have:



outer --> null
thirdInner --> 5, 6





share|improve this answer




















  • 3





    "Not referenced" is tricky - as everything is "not referenced" at some point. The correct language is "not reachable by hard references from a GC root".

    – Boris the Spider
    Apr 8 at 10:26











  • That's absolute correct and a good note! But I feel that it would unnecessarily complicate the answer.

    – Zabuza
    Apr 8 at 10:34











  • @BoristheSpider I think "not referenced" is a good choice of words, as everything is referenced in java, just possibly not necessarily from java objects but from the jvm internals (e.g. stack variables, loaded java.lang.Class instances, ...) (the latter cases being marked together as GC roots). So I'd still call it "referenced".

    – bwoebi
    Apr 8 at 12:25






  • 2





    "Not referenced" is not strictly correct. "Not referenced" implies that two objects which refer to each other, or one object which refers to itself, won't be deleted, which is obviously untrue. Garbage collectors don't delete objects that are "not referenced", they delete objects that are not reachable from a GC root (running thread). When null is assigned to outer, the entire array and all inner arrays become not reachable by the program, and all are swept away in one go. It's not necessary to delete the outer array first, in order to discover the inner arrays were also deletable.

    – Boann
    Apr 8 at 16:34






  • 1





    Be my guest and correct the terms. But I think it is better to keep it simple and then add a section which has a reference to some deeper explanation, quickly summarizing how it works in two sentences.

    – Zabuza
    yesterday



















13














At some point after the array goes out of scope, the garbage collector will reclaim the memory if there are no other references to it.



If you want to null your reference before the variable goes out of scope (keep in mind that if some other code has this reference, it won't get garbage collected):



Foo[][] fooArray = new Foo[2][3];

...

// this will null the reference to the array
fooArray = null;





share|improve this answer




















  • 3





    Your phrasing (“once”) makes it sounds as if the memory will be reclaimed as soon as the last reference goes out of scope. It’s a rather important property of the Java GC that this is not the case.

    – Konrad Rudolph
    Apr 8 at 9:24












  • Good point - edited.

    – Jason
    Apr 8 at 22:24


















2














Unlike C, Java provides automatic garbage collection,which will clear the array for you as it becomes unreachable(i.e goes out of scope).If you want you can make the array as null so that the memory location becomes unreachable.



 Foo[][] fooArray = new Foo[2][3];
.
.
.
fooArray = null;
System.gc();


This gc call doesn't ensure that JVM will run garbage collector but it suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects






share|improve this answer




















  • 4





    I do not really see the benefit of suggesting System#gc. In most situations it will degrade performance. Let the garbage collector do it's job. It is usually only used to cleanup before a measurement (profiler and other tools).

    – Zabuza
    Apr 8 at 7:15












  • I agree with you but in some cases, it may make sense to suggest to the JVM that it do a full collection NOW as you may know the application will be sitting idle for the next few minutes before heavy work

    – Vaibhav Gupta
    Apr 8 at 7:18






  • 5





    @Vaibhav Gupta The garbage collector does not clear the array, it only frees it. It is possible for malware to access the data, sometimes for a long time, until the memory has been reused. If the array contains sensitive data it should be actively cleared before being released.

    – Jonathan Rosenne
    Apr 8 at 7:52

















3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









15














Explanation



You can not explicitly delete something in Java. It is the garbage collectors job to do that. It will delete anything which is not used anymore by anyone. So either



  1. let the variable fall out of scope or

  2. assign null

  3. or any other instance to it.

Then the array instance (as well as its subarrays) is not referenced anymore and the garbage collector will delete it eventually.




References



To understand why re-assigning the outer array is enough to also delete the inner arrays, you need to understand how they are referenced. Again, the garbage collector will delete anything which is not referenced. So let's take a look at an array such as:



int[][] outer = 1, 2, 3, 4, 5, 6;


We have 4 array instances. One is of type int[][] and three of type int[]. Also, we have one variable outer. The instances are referenced as follows:



 ___> 1, 2
|
outer --> int[][] ---|---> 3, 4
|
|___> 5, 6


So by deleting outer, nobody references int[][] anymore. The garbage collector can now delete it. But that also removes all references to the inner arrays, so the garbage collector can now also delete them.



Now assume that you would reference one of the inner arrays by another variable:



int[][] outer = 1, 2, 3, 4, 5, 6;
int[] thirdInner = outer[2];
other = null; // remove the reference


The situation is now



outer --> null

___> 1, 2
|
int[][] ---|---> 3, 4
|
|______> 5, 6
|
thirdInner _______________|


So the garbage collector will now delete the outer array int[][], which also removes all references to the first and second inner array. But the third is still referenced by thirdInner, so after garbage collection we have:



outer --> null
thirdInner --> 5, 6





share|improve this answer




















  • 3





    "Not referenced" is tricky - as everything is "not referenced" at some point. The correct language is "not reachable by hard references from a GC root".

    – Boris the Spider
    Apr 8 at 10:26











  • That's absolute correct and a good note! But I feel that it would unnecessarily complicate the answer.

    – Zabuza
    Apr 8 at 10:34











  • @BoristheSpider I think "not referenced" is a good choice of words, as everything is referenced in java, just possibly not necessarily from java objects but from the jvm internals (e.g. stack variables, loaded java.lang.Class instances, ...) (the latter cases being marked together as GC roots). So I'd still call it "referenced".

    – bwoebi
    Apr 8 at 12:25






  • 2





    "Not referenced" is not strictly correct. "Not referenced" implies that two objects which refer to each other, or one object which refers to itself, won't be deleted, which is obviously untrue. Garbage collectors don't delete objects that are "not referenced", they delete objects that are not reachable from a GC root (running thread). When null is assigned to outer, the entire array and all inner arrays become not reachable by the program, and all are swept away in one go. It's not necessary to delete the outer array first, in order to discover the inner arrays were also deletable.

    – Boann
    Apr 8 at 16:34






  • 1





    Be my guest and correct the terms. But I think it is better to keep it simple and then add a section which has a reference to some deeper explanation, quickly summarizing how it works in two sentences.

    – Zabuza
    yesterday
















15














Explanation



You can not explicitly delete something in Java. It is the garbage collectors job to do that. It will delete anything which is not used anymore by anyone. So either



  1. let the variable fall out of scope or

  2. assign null

  3. or any other instance to it.

Then the array instance (as well as its subarrays) is not referenced anymore and the garbage collector will delete it eventually.




References



To understand why re-assigning the outer array is enough to also delete the inner arrays, you need to understand how they are referenced. Again, the garbage collector will delete anything which is not referenced. So let's take a look at an array such as:



int[][] outer = 1, 2, 3, 4, 5, 6;


We have 4 array instances. One is of type int[][] and three of type int[]. Also, we have one variable outer. The instances are referenced as follows:



 ___> 1, 2
|
outer --> int[][] ---|---> 3, 4
|
|___> 5, 6


So by deleting outer, nobody references int[][] anymore. The garbage collector can now delete it. But that also removes all references to the inner arrays, so the garbage collector can now also delete them.



Now assume that you would reference one of the inner arrays by another variable:



int[][] outer = 1, 2, 3, 4, 5, 6;
int[] thirdInner = outer[2];
other = null; // remove the reference


The situation is now



outer --> null

___> 1, 2
|
int[][] ---|---> 3, 4
|
|______> 5, 6
|
thirdInner _______________|


So the garbage collector will now delete the outer array int[][], which also removes all references to the first and second inner array. But the third is still referenced by thirdInner, so after garbage collection we have:



outer --> null
thirdInner --> 5, 6





share|improve this answer




















  • 3





    "Not referenced" is tricky - as everything is "not referenced" at some point. The correct language is "not reachable by hard references from a GC root".

    – Boris the Spider
    Apr 8 at 10:26











  • That's absolute correct and a good note! But I feel that it would unnecessarily complicate the answer.

    – Zabuza
    Apr 8 at 10:34











  • @BoristheSpider I think "not referenced" is a good choice of words, as everything is referenced in java, just possibly not necessarily from java objects but from the jvm internals (e.g. stack variables, loaded java.lang.Class instances, ...) (the latter cases being marked together as GC roots). So I'd still call it "referenced".

    – bwoebi
    Apr 8 at 12:25






  • 2





    "Not referenced" is not strictly correct. "Not referenced" implies that two objects which refer to each other, or one object which refers to itself, won't be deleted, which is obviously untrue. Garbage collectors don't delete objects that are "not referenced", they delete objects that are not reachable from a GC root (running thread). When null is assigned to outer, the entire array and all inner arrays become not reachable by the program, and all are swept away in one go. It's not necessary to delete the outer array first, in order to discover the inner arrays were also deletable.

    – Boann
    Apr 8 at 16:34






  • 1





    Be my guest and correct the terms. But I think it is better to keep it simple and then add a section which has a reference to some deeper explanation, quickly summarizing how it works in two sentences.

    – Zabuza
    yesterday














15












15








15







Explanation



You can not explicitly delete something in Java. It is the garbage collectors job to do that. It will delete anything which is not used anymore by anyone. So either



  1. let the variable fall out of scope or

  2. assign null

  3. or any other instance to it.

Then the array instance (as well as its subarrays) is not referenced anymore and the garbage collector will delete it eventually.




References



To understand why re-assigning the outer array is enough to also delete the inner arrays, you need to understand how they are referenced. Again, the garbage collector will delete anything which is not referenced. So let's take a look at an array such as:



int[][] outer = 1, 2, 3, 4, 5, 6;


We have 4 array instances. One is of type int[][] and three of type int[]. Also, we have one variable outer. The instances are referenced as follows:



 ___> 1, 2
|
outer --> int[][] ---|---> 3, 4
|
|___> 5, 6


So by deleting outer, nobody references int[][] anymore. The garbage collector can now delete it. But that also removes all references to the inner arrays, so the garbage collector can now also delete them.



Now assume that you would reference one of the inner arrays by another variable:



int[][] outer = 1, 2, 3, 4, 5, 6;
int[] thirdInner = outer[2];
other = null; // remove the reference


The situation is now



outer --> null

___> 1, 2
|
int[][] ---|---> 3, 4
|
|______> 5, 6
|
thirdInner _______________|


So the garbage collector will now delete the outer array int[][], which also removes all references to the first and second inner array. But the third is still referenced by thirdInner, so after garbage collection we have:



outer --> null
thirdInner --> 5, 6





share|improve this answer















Explanation



You can not explicitly delete something in Java. It is the garbage collectors job to do that. It will delete anything which is not used anymore by anyone. So either



  1. let the variable fall out of scope or

  2. assign null

  3. or any other instance to it.

Then the array instance (as well as its subarrays) is not referenced anymore and the garbage collector will delete it eventually.




References



To understand why re-assigning the outer array is enough to also delete the inner arrays, you need to understand how they are referenced. Again, the garbage collector will delete anything which is not referenced. So let's take a look at an array such as:



int[][] outer = 1, 2, 3, 4, 5, 6;


We have 4 array instances. One is of type int[][] and three of type int[]. Also, we have one variable outer. The instances are referenced as follows:



 ___> 1, 2
|
outer --> int[][] ---|---> 3, 4
|
|___> 5, 6


So by deleting outer, nobody references int[][] anymore. The garbage collector can now delete it. But that also removes all references to the inner arrays, so the garbage collector can now also delete them.



Now assume that you would reference one of the inner arrays by another variable:



int[][] outer = 1, 2, 3, 4, 5, 6;
int[] thirdInner = outer[2];
other = null; // remove the reference


The situation is now



outer --> null

___> 1, 2
|
int[][] ---|---> 3, 4
|
|______> 5, 6
|
thirdInner _______________|


So the garbage collector will now delete the outer array int[][], which also removes all references to the first and second inner array. But the third is still referenced by thirdInner, so after garbage collection we have:



outer --> null
thirdInner --> 5, 6






share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday









isanae

2,55711437




2,55711437










answered Apr 8 at 7:27









ZabuzaZabuza

12.1k52744




12.1k52744







  • 3





    "Not referenced" is tricky - as everything is "not referenced" at some point. The correct language is "not reachable by hard references from a GC root".

    – Boris the Spider
    Apr 8 at 10:26











  • That's absolute correct and a good note! But I feel that it would unnecessarily complicate the answer.

    – Zabuza
    Apr 8 at 10:34











  • @BoristheSpider I think "not referenced" is a good choice of words, as everything is referenced in java, just possibly not necessarily from java objects but from the jvm internals (e.g. stack variables, loaded java.lang.Class instances, ...) (the latter cases being marked together as GC roots). So I'd still call it "referenced".

    – bwoebi
    Apr 8 at 12:25






  • 2





    "Not referenced" is not strictly correct. "Not referenced" implies that two objects which refer to each other, or one object which refers to itself, won't be deleted, which is obviously untrue. Garbage collectors don't delete objects that are "not referenced", they delete objects that are not reachable from a GC root (running thread). When null is assigned to outer, the entire array and all inner arrays become not reachable by the program, and all are swept away in one go. It's not necessary to delete the outer array first, in order to discover the inner arrays were also deletable.

    – Boann
    Apr 8 at 16:34






  • 1





    Be my guest and correct the terms. But I think it is better to keep it simple and then add a section which has a reference to some deeper explanation, quickly summarizing how it works in two sentences.

    – Zabuza
    yesterday













  • 3





    "Not referenced" is tricky - as everything is "not referenced" at some point. The correct language is "not reachable by hard references from a GC root".

    – Boris the Spider
    Apr 8 at 10:26











  • That's absolute correct and a good note! But I feel that it would unnecessarily complicate the answer.

    – Zabuza
    Apr 8 at 10:34











  • @BoristheSpider I think "not referenced" is a good choice of words, as everything is referenced in java, just possibly not necessarily from java objects but from the jvm internals (e.g. stack variables, loaded java.lang.Class instances, ...) (the latter cases being marked together as GC roots). So I'd still call it "referenced".

    – bwoebi
    Apr 8 at 12:25






  • 2





    "Not referenced" is not strictly correct. "Not referenced" implies that two objects which refer to each other, or one object which refers to itself, won't be deleted, which is obviously untrue. Garbage collectors don't delete objects that are "not referenced", they delete objects that are not reachable from a GC root (running thread). When null is assigned to outer, the entire array and all inner arrays become not reachable by the program, and all are swept away in one go. It's not necessary to delete the outer array first, in order to discover the inner arrays were also deletable.

    – Boann
    Apr 8 at 16:34






  • 1





    Be my guest and correct the terms. But I think it is better to keep it simple and then add a section which has a reference to some deeper explanation, quickly summarizing how it works in two sentences.

    – Zabuza
    yesterday








3




3





"Not referenced" is tricky - as everything is "not referenced" at some point. The correct language is "not reachable by hard references from a GC root".

– Boris the Spider
Apr 8 at 10:26





"Not referenced" is tricky - as everything is "not referenced" at some point. The correct language is "not reachable by hard references from a GC root".

– Boris the Spider
Apr 8 at 10:26













That's absolute correct and a good note! But I feel that it would unnecessarily complicate the answer.

– Zabuza
Apr 8 at 10:34





That's absolute correct and a good note! But I feel that it would unnecessarily complicate the answer.

– Zabuza
Apr 8 at 10:34













@BoristheSpider I think "not referenced" is a good choice of words, as everything is referenced in java, just possibly not necessarily from java objects but from the jvm internals (e.g. stack variables, loaded java.lang.Class instances, ...) (the latter cases being marked together as GC roots). So I'd still call it "referenced".

– bwoebi
Apr 8 at 12:25





@BoristheSpider I think "not referenced" is a good choice of words, as everything is referenced in java, just possibly not necessarily from java objects but from the jvm internals (e.g. stack variables, loaded java.lang.Class instances, ...) (the latter cases being marked together as GC roots). So I'd still call it "referenced".

– bwoebi
Apr 8 at 12:25




2




2





"Not referenced" is not strictly correct. "Not referenced" implies that two objects which refer to each other, or one object which refers to itself, won't be deleted, which is obviously untrue. Garbage collectors don't delete objects that are "not referenced", they delete objects that are not reachable from a GC root (running thread). When null is assigned to outer, the entire array and all inner arrays become not reachable by the program, and all are swept away in one go. It's not necessary to delete the outer array first, in order to discover the inner arrays were also deletable.

– Boann
Apr 8 at 16:34





"Not referenced" is not strictly correct. "Not referenced" implies that two objects which refer to each other, or one object which refers to itself, won't be deleted, which is obviously untrue. Garbage collectors don't delete objects that are "not referenced", they delete objects that are not reachable from a GC root (running thread). When null is assigned to outer, the entire array and all inner arrays become not reachable by the program, and all are swept away in one go. It's not necessary to delete the outer array first, in order to discover the inner arrays were also deletable.

– Boann
Apr 8 at 16:34




1




1





Be my guest and correct the terms. But I think it is better to keep it simple and then add a section which has a reference to some deeper explanation, quickly summarizing how it works in two sentences.

– Zabuza
yesterday






Be my guest and correct the terms. But I think it is better to keep it simple and then add a section which has a reference to some deeper explanation, quickly summarizing how it works in two sentences.

– Zabuza
yesterday














13














At some point after the array goes out of scope, the garbage collector will reclaim the memory if there are no other references to it.



If you want to null your reference before the variable goes out of scope (keep in mind that if some other code has this reference, it won't get garbage collected):



Foo[][] fooArray = new Foo[2][3];

...

// this will null the reference to the array
fooArray = null;





share|improve this answer




















  • 3





    Your phrasing (“once”) makes it sounds as if the memory will be reclaimed as soon as the last reference goes out of scope. It’s a rather important property of the Java GC that this is not the case.

    – Konrad Rudolph
    Apr 8 at 9:24












  • Good point - edited.

    – Jason
    Apr 8 at 22:24















13














At some point after the array goes out of scope, the garbage collector will reclaim the memory if there are no other references to it.



If you want to null your reference before the variable goes out of scope (keep in mind that if some other code has this reference, it won't get garbage collected):



Foo[][] fooArray = new Foo[2][3];

...

// this will null the reference to the array
fooArray = null;





share|improve this answer




















  • 3





    Your phrasing (“once”) makes it sounds as if the memory will be reclaimed as soon as the last reference goes out of scope. It’s a rather important property of the Java GC that this is not the case.

    – Konrad Rudolph
    Apr 8 at 9:24












  • Good point - edited.

    – Jason
    Apr 8 at 22:24













13












13








13







At some point after the array goes out of scope, the garbage collector will reclaim the memory if there are no other references to it.



If you want to null your reference before the variable goes out of scope (keep in mind that if some other code has this reference, it won't get garbage collected):



Foo[][] fooArray = new Foo[2][3];

...

// this will null the reference to the array
fooArray = null;





share|improve this answer















At some point after the array goes out of scope, the garbage collector will reclaim the memory if there are no other references to it.



If you want to null your reference before the variable goes out of scope (keep in mind that if some other code has this reference, it won't get garbage collected):



Foo[][] fooArray = new Foo[2][3];

...

// this will null the reference to the array
fooArray = null;






share|improve this answer














share|improve this answer



share|improve this answer








edited Apr 8 at 22:24

























answered Apr 8 at 7:08









JasonJason

9,69733545




9,69733545







  • 3





    Your phrasing (“once”) makes it sounds as if the memory will be reclaimed as soon as the last reference goes out of scope. It’s a rather important property of the Java GC that this is not the case.

    – Konrad Rudolph
    Apr 8 at 9:24












  • Good point - edited.

    – Jason
    Apr 8 at 22:24












  • 3





    Your phrasing (“once”) makes it sounds as if the memory will be reclaimed as soon as the last reference goes out of scope. It’s a rather important property of the Java GC that this is not the case.

    – Konrad Rudolph
    Apr 8 at 9:24












  • Good point - edited.

    – Jason
    Apr 8 at 22:24







3




3





Your phrasing (“once”) makes it sounds as if the memory will be reclaimed as soon as the last reference goes out of scope. It’s a rather important property of the Java GC that this is not the case.

– Konrad Rudolph
Apr 8 at 9:24






Your phrasing (“once”) makes it sounds as if the memory will be reclaimed as soon as the last reference goes out of scope. It’s a rather important property of the Java GC that this is not the case.

– Konrad Rudolph
Apr 8 at 9:24














Good point - edited.

– Jason
Apr 8 at 22:24





Good point - edited.

– Jason
Apr 8 at 22:24











2














Unlike C, Java provides automatic garbage collection,which will clear the array for you as it becomes unreachable(i.e goes out of scope).If you want you can make the array as null so that the memory location becomes unreachable.



 Foo[][] fooArray = new Foo[2][3];
.
.
.
fooArray = null;
System.gc();


This gc call doesn't ensure that JVM will run garbage collector but it suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects






share|improve this answer




















  • 4





    I do not really see the benefit of suggesting System#gc. In most situations it will degrade performance. Let the garbage collector do it's job. It is usually only used to cleanup before a measurement (profiler and other tools).

    – Zabuza
    Apr 8 at 7:15












  • I agree with you but in some cases, it may make sense to suggest to the JVM that it do a full collection NOW as you may know the application will be sitting idle for the next few minutes before heavy work

    – Vaibhav Gupta
    Apr 8 at 7:18






  • 5





    @Vaibhav Gupta The garbage collector does not clear the array, it only frees it. It is possible for malware to access the data, sometimes for a long time, until the memory has been reused. If the array contains sensitive data it should be actively cleared before being released.

    – Jonathan Rosenne
    Apr 8 at 7:52















2














Unlike C, Java provides automatic garbage collection,which will clear the array for you as it becomes unreachable(i.e goes out of scope).If you want you can make the array as null so that the memory location becomes unreachable.



 Foo[][] fooArray = new Foo[2][3];
.
.
.
fooArray = null;
System.gc();


This gc call doesn't ensure that JVM will run garbage collector but it suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects






share|improve this answer




















  • 4





    I do not really see the benefit of suggesting System#gc. In most situations it will degrade performance. Let the garbage collector do it's job. It is usually only used to cleanup before a measurement (profiler and other tools).

    – Zabuza
    Apr 8 at 7:15












  • I agree with you but in some cases, it may make sense to suggest to the JVM that it do a full collection NOW as you may know the application will be sitting idle for the next few minutes before heavy work

    – Vaibhav Gupta
    Apr 8 at 7:18






  • 5





    @Vaibhav Gupta The garbage collector does not clear the array, it only frees it. It is possible for malware to access the data, sometimes for a long time, until the memory has been reused. If the array contains sensitive data it should be actively cleared before being released.

    – Jonathan Rosenne
    Apr 8 at 7:52













2












2








2







Unlike C, Java provides automatic garbage collection,which will clear the array for you as it becomes unreachable(i.e goes out of scope).If you want you can make the array as null so that the memory location becomes unreachable.



 Foo[][] fooArray = new Foo[2][3];
.
.
.
fooArray = null;
System.gc();


This gc call doesn't ensure that JVM will run garbage collector but it suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects






share|improve this answer















Unlike C, Java provides automatic garbage collection,which will clear the array for you as it becomes unreachable(i.e goes out of scope).If you want you can make the array as null so that the memory location becomes unreachable.



 Foo[][] fooArray = new Foo[2][3];
.
.
.
fooArray = null;
System.gc();


This gc call doesn't ensure that JVM will run garbage collector but it suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects







share|improve this answer














share|improve this answer



share|improve this answer








edited Apr 8 at 7:15

























answered Apr 8 at 7:14









Vaibhav GuptaVaibhav Gupta

475311




475311







  • 4





    I do not really see the benefit of suggesting System#gc. In most situations it will degrade performance. Let the garbage collector do it's job. It is usually only used to cleanup before a measurement (profiler and other tools).

    – Zabuza
    Apr 8 at 7:15












  • I agree with you but in some cases, it may make sense to suggest to the JVM that it do a full collection NOW as you may know the application will be sitting idle for the next few minutes before heavy work

    – Vaibhav Gupta
    Apr 8 at 7:18






  • 5





    @Vaibhav Gupta The garbage collector does not clear the array, it only frees it. It is possible for malware to access the data, sometimes for a long time, until the memory has been reused. If the array contains sensitive data it should be actively cleared before being released.

    – Jonathan Rosenne
    Apr 8 at 7:52












  • 4





    I do not really see the benefit of suggesting System#gc. In most situations it will degrade performance. Let the garbage collector do it's job. It is usually only used to cleanup before a measurement (profiler and other tools).

    – Zabuza
    Apr 8 at 7:15












  • I agree with you but in some cases, it may make sense to suggest to the JVM that it do a full collection NOW as you may know the application will be sitting idle for the next few minutes before heavy work

    – Vaibhav Gupta
    Apr 8 at 7:18






  • 5





    @Vaibhav Gupta The garbage collector does not clear the array, it only frees it. It is possible for malware to access the data, sometimes for a long time, until the memory has been reused. If the array contains sensitive data it should be actively cleared before being released.

    – Jonathan Rosenne
    Apr 8 at 7:52







4




4





I do not really see the benefit of suggesting System#gc. In most situations it will degrade performance. Let the garbage collector do it's job. It is usually only used to cleanup before a measurement (profiler and other tools).

– Zabuza
Apr 8 at 7:15






I do not really see the benefit of suggesting System#gc. In most situations it will degrade performance. Let the garbage collector do it's job. It is usually only used to cleanup before a measurement (profiler and other tools).

– Zabuza
Apr 8 at 7:15














I agree with you but in some cases, it may make sense to suggest to the JVM that it do a full collection NOW as you may know the application will be sitting idle for the next few minutes before heavy work

– Vaibhav Gupta
Apr 8 at 7:18





I agree with you but in some cases, it may make sense to suggest to the JVM that it do a full collection NOW as you may know the application will be sitting idle for the next few minutes before heavy work

– Vaibhav Gupta
Apr 8 at 7:18




5




5





@Vaibhav Gupta The garbage collector does not clear the array, it only frees it. It is possible for malware to access the data, sometimes for a long time, until the memory has been reused. If the array contains sensitive data it should be actively cleared before being released.

– Jonathan Rosenne
Apr 8 at 7:52





@Vaibhav Gupta The garbage collector does not clear the array, it only frees it. It is possible for malware to access the data, sometimes for a long time, until the memory has been reused. If the array contains sensitive data it should be actively cleared before being released.

– Jonathan Rosenne
Apr 8 at 7:52



Popular posts from this blog

Bulk add to cart function issuecart vs. mini cart issue … rwd themeRedirect Add to cart button to cart pageAdd to cart issue - Magento 2.1The requested Payment Method is not available When creating an orderM2: reason add-to-cart might not function in production modeAdd to cart issue in some android devicesMagento 2 - custom price can not add to subtotal and grand total after add to cartAdd to cart codeIssue with my cart module on pdp and cart pages, just keeps spinningBulk price and quantity update using rest api

Magento2 - How to hide price filter only in specific categories?Multiselect price filter attribute in layered navigationhide only some categories from layered navigation in magentoRemove Price Filter on certain categoriescustomize layered price filter?Hide Price for a particular customer groupPrice filter in layered navigation not working correctly with price including tax in magento 2.2.3Magento 2 how to hide attribute at Layered navigation?Magento 2. how to hide price only for specific categoriesMagento 2 How can I hide the price and total from cart and checkout summary?Magento2: Can we add navigation layered filter like price filter for other attribute?