4

I have read about making an object explicitly garbage collected, that in many ways. So i wanted to know some of the methods of making it explicitly garbage collected

2
  • What language and implementation are you referring to? There are a lot of GC languages out there, and every implementation may be free to add its own non-standard features. Commented Feb 4, 2010 at 19:42
  • Sorry for that. but i have selected only one tag, java. it means its related to java language Commented Feb 5, 2010 at 18:34

4 Answers 4

6

There is no way for explicit garbage collection.

You can "politely ask" the virtual machine to do garbage collection by calling:

System.gc(); 

but it is not guaranteed it will.

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.

and "best effort" might be to postpone the garbage collection.

For how to make objects elligible for garbage collection read Effective Java, Chapter 2

Sign up to request clarification or add additional context in comments.

1 Comment

I love the term "best effort". It pretty much means "maybe, if I feel like it".
2

You can explicitly make an object eligible for garbage collection by setting all references to it to null.

This won't call the garbage collector itself, but when the collection does start, it will collect this object.

You're best off letting the Java garbage collector doing it all automatically by itself. It has been optimized to the point where it'll be better and more efficient than anything you'd ever want to probably do.

Comments

1

System.gc is to tell the JVM to execute the garbage collector explicitly.

But as for as some methods which i know to make an object explicitly garbage collected are:

Assigning its reference to point to null EX: Animal a = new Animal(); a = null

Assigning its reference to point to another object. EX: Animal a1 = new Animal(); a1 = new Animal();

1 Comment

Assigning all references to an object to null, only makes the object ready to be garbage collected. It does not make it garbage collected immediately, and it can stay on in memory for a long time afterwards. Calling System.gc() also does not guarantee all unreferenced objects will be collected. Read the other answers.
1

You shouldn't worry about forcing a GC unless you're writing, say, a tool like VisualVM yourself.

NetBeans, IntelliJ, VisualVM, and a great many others can all force a GC, not hint, but really force. Using JVMTI you can force a GC.

BUT once again you probably do NOT want to do that.

You force a GC (not hint) using JVMTI's ForceGarbageCollection.

That said, you probably really DO NOT want to do that (it really bares repeating).

Authoritative info on the subject if you really want to know (but you probably don't) how to force, for a fact, a GC:

http://java.sun.com/javase/6/docs/platform/jvmti/jvmti.html#ForceGarbageCollection

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.