Java垃圾回收机制gc,自动回收没有引用的对象,释放内存。
示例代码:
- class Finalize{
- public static void main(String args[]){
- int count;
- FDemo ob=new FDemo(0);
- for(count=1;count<10000000;count++){
- ob.generator(count); //创建大量对象
- }
- }
- }
- class FDemo{
- int x;
- FDemo(int i){
- x=i;
- }
- protected void finalize(){ //finalize()方法,在对象被回收之前调用,确保对象完全终止,
- System.out.println("finalize "+x);
- }
- void generator(int i){
- FDemo o=new FDemo(i);
- }
- }