Are strings garbage collected?

Are locally created Java arrays (inside for or while loop) garbage collected automatically or does it result in memory leaks?

  • Consider the following code: [code] int testCases = readInt(); // This array stores precomputed prime numbers in range [m, n] boolean[] primes = null; int m = 0; int n = 0; for(int t=0; t<testCases; t++) { m = readInt(); n = readInt(); // primes array will be created testCases times primes = new boolean[n-m+1]; // Do I need to uncomment following statement or primes array will be garbage collected automatically? // primes = null; } [/code] How can I ensure that above code won't result in memory leaks for larger values of testCases?

  • Answer:

    They will be garbage collected since there is no external reference to them. Java, and most other languages that manage memory automatically, use a link count or reference count to determine when memory can be freed. Every  time you assign the address of a variable or allocation the reference count is incremented. When that variable goes out of scope, the refernece count is decremented. When the garbage collection routine runs, all memory allocations with a reference count of 0 can be safely freed. All memory associated with the arrays created inside the loop will have a reference count of at least one because they are scoped to the loop. If you assign elements of that array to other loop scoped variables the refence count will go up but those variables are also scoped to the array so when they fall out of scope the reference count will be decremented. As soon as the loop finishes everything created within it falls out of scope, the final counts will go to zero. The next call of the garbage collection routine will mark the memory as free.

Mick Stute at Quora Visit the source

Was this solution helpful to you?

Other answers

In the given use case, java will do garbage collection automatically. As soon as all the variables (i.e. pointers) lost references to the object, java will garbage collect them automatically. Only the following types of scenarios  the garbage collection won't happen : String[] stack = new String[100]; int pointer = 0; while(true){    if( condition statisfied){       stack[pointer] = "Hello World "+i;       pointer++;   }   else {         pointer--;   } } Here you are just reducing the pointer, but array element is still pointing the new Boolean object. In this case Java would never know that you released the object until and unless you override it again.

Sumit Tiwari

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.