java内存泄漏gc内存是否测试方法

package com.github.pig.auth;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


class person {
    public String name;
    public String age;
    private hand handhello;

    public person() {
        handhello = new hand(this);
    }

    public void sayhello() {
        handhello.Shake();
    }

    public void finalize() {
        System.out.println("gc person!");
    }
}

class hand {
    private person per;

    public hand(person per) {
        this.per = per;
    }

    public void Shake() {
        System.out.println("Shake!");
    }

    public void finalize() {
        System.out.println("gc hand!");
    }
}

public class Test {


    public static void main(String[] args) throws IOException, InterruptedException {
        Vector<person> v = new Vector<person>();
        for (int i = 0; i < 5; i++) {
            person o = new person();
            v.add(o);
        }
        v.clear();

        System.out.println("clear");
        TimeUnit.MILLISECONDS.sleep(1000);
        System.out.println("gc");

        System.gc();


        TimeUnit.MILLISECONDS.sleep(1000);
        System.out.println("内存全部释放");
    }



}



clear
gc
gc hand!
gc person!
gc hand!
gc person!
gc hand!
gc person!
gc hand!
gc person!
gc hand!
gc person!
内存全部释放

猜你喜欢

转载自blog.csdn.net/hewei314599782/article/details/80150025