-3

Does it happen automatically? How do can run it?

jjnguy
  • 128,890
  • 51
  • 289
  • 321
TIMEX
  • 217,272
  • 324
  • 727
  • 1,038
  • The thing is, lots of people who are newer to the language really enjoy and learn from the process of answering questions like this. It would be nice if people would just leave questions like this to be answered by people who can learn by interacting with each other. A lot of engineers really don't get that learning through teaching may be the most important part of this site. – Bill K Mar 21 '11 at 19:42

5 Answers5

9

Yes, garbage colelction happens automatically. You should not need to manually run it, nor is it recommended. The whole point of garbage collection is that it should be transparent.

Please see: Tuning Garbage Collection with the 5.0 Java[tm] Virtual Machine

[BTW, there are many questions on SO related to java garbage collection]

Mitch Wheat
  • 280,588
  • 41
  • 444
  • 526
3

Yes it does. You can run System.gc() but it is not recommended. Also you still can have memory leaks

jjnguy
  • 128,890
  • 51
  • 289
  • 321
Bostone
  • 34,822
  • 38
  • 158
  • 216
  • 2
    It's really not recommended to run System.gc() (to reiterate). See some discussion on SO here - http://stackoverflow.com/questions/66540/system-gc-in-java/66692 – JasCav Feb 15 '10 at 01:01
  • Do I have to run that System#gc thing...or will it just happen automatically / – TIMEX Feb 15 '10 at 01:05
  • Automatically given that no one hold the reference to released object. Calling System#gc is redundant and unreliable at best – Bostone Feb 15 '10 at 01:15
  • Calling `System.gc()` gives the JVM only a *hint* that it should run GC. It does not guarantee that it will actually run GC. Just write efficient code and don't worry about GC's task. – BalusC Feb 15 '10 at 01:55
2

The garbage collector will automatically collect when it needs to. No need to do anything yourself unless you really have to.

nalo
  • 868
  • 2
  • 8
  • 12
2

With the Java Virtual Machine, data (objects, arrays of primitives) is stored in the heap which is a shared region of memory that all JVM thread can access. Memory is allocated for the heap when the JVM starts (and may expand to a certain limit at runtime depending on the configuration). Whenever a new object is created, a portion of the heap is allocated to store it.

When the heap is full i.e. when no further allocations can be done (this is an over simplified version, I'm skipping details), the garbage collector is started automatically to reclaim memory space. Basically, any object which is not referenced by an active thread may be safely de-allocated.

Note that the garbage collector thread normally runs as a very low process thread but, once kicked in, it can not be suspended until the task completes.

Pascal Thivent
  • 535,937
  • 127
  • 1,027
  • 1,106
0

Java does have automatic garbage collection. However there are some things you need to do. Java only GCs those objects in the heap that have no reference. What you do is assign null value the variable you no longer require and want the memory in heap to be freed. If you do not assign null to the variables, the objects in the heap will have a reference and will occupy the memory space even if you do not need them anymore.

linuS
  • 171
  • 1
  • 6