I was intrigued by the answer to a similar question. I believe it is incorrect. So I created some test code. My question is, does this code prove/disprove/inconclusive the hypothesis that it is useful to nullify member variables in teardown methods? I tested it with JUnit4.8.1.
JUnit creates a new instance of the test class for each of the 4 tests. Each instance contains an Object obj. This obj is also inserted as the key of a static WeakHashMap. If and when JUnit releases its references to a test instance, the associated obj value will become weakly referenced and thus eligible for gc. The test tries to force a gc. The size of the WeakHashMap will tell me whether or not the objs are gc'ed. Some tests nullified the obj variable and others did not.
import org . junit . Before ;
import org . junit . After ;
import org . junit . Test ;
import java . util . ArrayList ;
import java . util . WeakHashMap ;
import java . util . concurrent . atomic . AtomicInteger ;
import static org . junit . Assert . * ;
public class Memory
{
static AtomicInteger idx = new AtomicInteger ( 0 ) ;
static WeakHashMap < Object , Object > map = new WeakHashMap < Object , Object > ( ) ;
int id ;
Object obj ;
boolean nullify ;
public Memory ( )
{
super ( ) ;
}
@ Before
public void before ( )
{
id = idx . getAndIncrement ( ) ;
obj = new Object ( ) ;
map . put ( obj , new Object ( ) ) ;
System . out . println ( "<BEFORE TEST " + id + ">" ) ;
}
void test ( boolean n )
{
nullify = n ;
int before = map . size ( ) ;
gc ( ) ;
int after = map . size ( ) ;
System . out . println ( "BEFORE=" + before + "\tAFTER=" + after ) ;
}
@ Test
public void test0 ( )
{
test ( true ) ;
}
@ Test
public void test1 ( )
{
test ( false ) ;
}
@ Test
public void test2 ( )
{
test ( true ) ;
}
@ Test
public void test3 ( )
{
test ( false ) ;
}
@ After
public void after ( )
{
if ( nullify )
{
System . out . println ( "Nullifying obj" ) ;
obj = null ;
}
System . out . println ( "<AFTER TEST " + id + ">" ) ;
}
/**
* Try to force a gc when one is not really needed.
**/
void gc ( )
{
ArrayList < Object > waste = new ArrayList < Object > ( ) ;
System . gc ( ) ; // only a suggestion but I'll try to force it
list :
while ( true ) // try to force a gc
{
try
{
waste . add ( new Object ( ) ) ;
}
catch ( OutOfMemoryError cause )
{
// gc forced? should have been
waste = null ;
break list ;
}
}
System . gc ( ) ; // only a suggestion but I tried to force it
}
}
I ran the code using the command line interface (utilizing the -Xmx128k option to increase garbage collection) and got the following result
.<BEFORE TEST 0>
BEFORE=1 AFTER=1
Nullifying obj
<AFTER TEST 0>
.<BEFORE TEST 1>
BEFORE=2 AFTER=1
<AFTER TEST 1>
.<BEFORE TEST 2>
BEFORE=2 AFTER=1
Nullifying obj
<AFTER TEST 2>
.<BEFORE TEST 3>
BEFORE=2 AFTER=1
<AFTER TEST 3>
The Test0 obj was nullified and in Test1 it is gc'ed. But the Test1 obj was not nullified and it got gc'ed in Test2. This suggests that nullifying objects is not necessary.