java - Is Java7 intelligent in collecting objects if they are not used further in a scope, although the scope has not completely ended -


consider following code:

public class bitsettest {     public static void main(final string[] args) throws ioexception     {         system.out.println("start?");         int ch = system.in.read();          list<integer> numbers = getsortednumbers();          system.out.println("generated numbers");         ch = system.in.read();          rangeset<integer> rangeset = treerangeset.create();         (integer number : numbers)         {             rangeset.add(range.closed(number, number));         }          system.out.println("finished rangeset");         ch = system.in.read();          bitset bitset = new bitset();         (integer number : numbers)         {             bitset.set(number.intvalue());         }          system.out.println("finished bitset");         ch = system.in.read();          //system.out.println(numbers.size());         //system.out.println(rangeset.isempty());         //system.out.println(bitset.size());     }      private static list<integer> getsortednumbers()     {         int max = 200000000;         int n = max / 10;         list<integer> numbers = lists.newarraylistwithexpectedsize(max);         file file = new file("numbers.txt");         if (file.exists())         {             try (bufferedreader reader = new bufferedreader(new filereader(file)))             {                 string line = reader.readline();                 while ((line = reader.readline()) != null)                 {                     numbers.add(integer.valueof(line));                 }             }             catch (ioexception e1)             {                 throw new runtimeexception(e1);             }         }         else         {             (int = 0; < n; i++)             {                 int number = (int) (math.random() * max);                 numbers.add(number);                  if (i > 0 && % 10000 == 0)                 {                     system.out.println(i);                 }             }             collections.sort(numbers);             try (bufferedwriter writer = new bufferedwriter(new filewriter(file)))             {                 writer.write(numbers.get(0) + "");                 (int = 1; < n; i++)                 {                     writer.write("\n");                     writer.write(numbers.get(i) + "");                 }             }             catch (ioexception e1)             {                 throw new runtimeexception(e1);             }         }          return numbers;     } } 

at first pause(system.in.read()), jconsole shows memory usage 4mb. @ second pause("generated numbers"), since large list instantiated, memory usage jumps 922mb. @ next pause ("finished rangeset"), after running gc memory comes 4mb means list collected although function has not ended in scope.

when commented sys outs uncommented , used, list not collected till sysout gets executed.

just wanted understand whether jvm intelligent enough determine scope of object based on point not being used further?

garbage collection based on generations (there changes in java 8). until java 8 memory used divided 3 parts: young generation, old generation , permgen. newly created objects young generation, , if still available after time migrated old generation. permgen used used jvm's own data. garbage collection of young generation called minor garbage collection , happens relatively frequently.

java approach garbage collection "mark , swipe" (see first link) , marks objects not referenced life code dead , clean them (swipe).

in particular case following happening:

  1. java loads class young memory generation;
  2. java kicks off main class;
  3. your code allocates more memory in young generation;
  4. while code runs these memory referenced life code therefore if not cleaned;
  5. your program stops running;
  6. garbage collection kicks in , detects data, , class not referenced longer life code, therefore marks deletion , cleanup.

based on saying there chance class , data doesn't make old generation.

to more clear: garbage collection happening in parallel code , therefore can detect data not referenced more. assuming detects if object no longer referenced based on method end not correct (and proven test).


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -