I want to count how many times garbage collector call my finalize method, but i don't know how to implement global variable to do this. I've tried this way:
class ObjMaker {
int obj_nr;
ObjMaker(int obj_nr){
this.obj_nr = obj_nr;
}
protected void finalize(){
Global.finalize_counter += 1;
}
}
public class Global{
public int finalize_counter = 1;
}
class FinalizeSimpleDemo{
public static void main(String args[]){
for(int i = 1; i <= 10000000; i++){
ObjMaker ob = new ObjMaker(i);
}
}
}
but this code doesn't work and even doesn't look nice. What is a clever way to do this?