-1

My code is:

class Test{
    Test c=new Text();
    System.out.println(c.size());
    System.gc();
}

Can programmer use System.gc() for garbage collection in java? Is it preferrable? JVM performs automatically, then why should programmer to call System.gc()?

user987339
  • 10,519
  • 8
  • 40
  • 45
Pinky
  • 683
  • 1
  • 9
  • 20
  • 3
    see http://stackoverflow.com/questions/2414105/why-is-it-a-bad-practice-to-call-system-gc – Mureinik Oct 11 '13 at 10:39
  • Basically the only reason to ever call it is for simplify a bit debugging obscure memory leaks, and it *still* does not guarantee the time of the full gc so it can't be relied on. – kiheru Oct 11 '13 at 10:50

4 Answers4

2

You can call it. There will be no harm in that. But there is no gaurentee that the memory of object you are expecting immediately gets free or not.

More over JVM runs GC asynchronously and we need not to drive it. JVM intelligent enough to free memory.

Just for knowing purpose it is OK, If you are really thinking about to clear memory due to XYZ reason, definitely a design flaw is there in your programm structure.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
2

System.gc() sends a request to the GC to perform a collection cycle. This request may be served or it may be ignored, therefore neither result should be relied on.

A garbage collection cycle will happen automatically (without any action on your part), usually when the generation responsible for allocation of new objects is full or an allocation request cannot be satisfied at all.

In most cases, you should not need to call System.gc() at all in your code. System.gc() should be used in a few cases in which conditions similar to the following apply:

  • You know that a large amount of memory has just become unreachable.
  • It is essential that this amount of memory be freed quickly.
    • Or your program is about to enter a time-critical state where a GC cycle should happen as late as possible (or not at all) and so it helps to perform a GC cycle before you enter that state.
  • You have at least a rough idea of how the GC of the target environment works.
  • You have verified that the strategy of that GC is not optimal for your scenario at that point.
Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104
1

Even if you use System.gc() there is no guarantee that memory will be freed

from the oracle site Calling the gc method 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.

SpringLearner
  • 13,738
  • 20
  • 78
  • 116
1

You can call it, but is not guarantee that memory will be freed. Furthermore, if the memory was released, this could have negative consequences for the execution of your program. I will try to explain it to you, but I noticed you that my english is not very good XD

Java heap memory is divided in three zones based on objects generations. Oversimplifying: young, adult and old. When occur an invocation to GC, first it do is to check "young zone" for unused objects and liberate them. If GC doesn't free enought memory in the "young zone", it examine "adult zone". If GC doesn't free enought memory in the "adult zone", it examine the "old zone". Each generation is more costly for examine by GC than the last.

Well, objects are initialy created in young zone, if GC perform an execution in the young zone and the object is still used, that object pass to adult zone. Idem for adult -> old zones. If you invoke an execution of GC, it can think that an young object is candidate for adult object, and move it to adult zone. This causes your adult zone grows in an unnecesary way. Later, when GC have to examine adult zone, the operation is more costly for him, and your program performance can go down.

angel_navarro
  • 1,757
  • 10
  • 11