D
Dan
Hello Java Guru's!
Is there a way to force garbage collection in Java?
Before anyone answers I feel I need to state:
- I know about System.gc() and Runtime.gc() and what they do
- I am not suggesting to do this in production code
The purpose of my asking is that I want to test a finializer during a
unit test by allowing something to go out of scope naturally. The
reason why I need it to fall out of scope instead of calling the
finalizer directly is because I am testing that no other references
exist and that the act of letting it fall out of scope makes it
eligible for gc.
So in this specific case, how would I do it? I know that code
profiling tools such as jProfiler can somehow force gc on a remote
JVM. Does anyone know how this works?
In my test cases, the following seems to run it every time... but I
was hoping that there was a more definitive way to do it:
System.gc();
Thread.sleep(1000);
Thank you!
-Dan
PS: I know there is another topic similar to this, but this isn't the
exact same question and I didn't want to hijack the thread.
Is there a way to force garbage collection in Java?
Before anyone answers I feel I need to state:
- I know about System.gc() and Runtime.gc() and what they do
- I am not suggesting to do this in production code
The purpose of my asking is that I want to test a finializer during a
unit test by allowing something to go out of scope naturally. The
reason why I need it to fall out of scope instead of calling the
finalizer directly is because I am testing that no other references
exist and that the act of letting it fall out of scope makes it
eligible for gc.
So in this specific case, how would I do it? I know that code
profiling tools such as jProfiler can somehow force gc on a remote
JVM. Does anyone know how this works?
In my test cases, the following seems to run it every time... but I
was hoping that there was a more definitive way to do it:
System.gc();
Thread.sleep(1000);
Thank you!
-Dan
PS: I know there is another topic similar to this, but this isn't the
exact same question and I didn't want to hijack the thread.