java 弱引用的内存泄漏模拟 及解决方案 参考ThreadLocalMap

下面这个Demo可以复现内存泄漏的场景.
从打印的日志可以看到在我们手动GC之后,Entry里面的value还是没有被回收。

package com.jalen.android.memoryLeak;

import java.lang.ref.WeakReference;
import java.util.Arrays;

/**
 * 测试弱引用的内存泄漏 参考ThreadLocalMap
 */
public class TestWeakReference {


    public static void main(String[] args) {
        MyWeakMap myWeakMap = new MyWeakMap();
        myWeakMap.put(0, new String("a"), "1");
        myWeakMap.put(1, new String("b"), "2");
        myWeakMap.put(2, new String("c"), "3");
        myWeakMap.put(3, new String("d"), "4");
        System.out.println(myWeakMap); //[a:1, b:2, c:3, d:4]
        System.gc();
        System.out.println(myWeakMap.get("a"));//null
        System.out.println(myWeakMap.get("b"));//null
        System.out.println(myWeakMap.get("c"));//null
        System.out.println(myWeakMap.get("d"));//null
        System.out.println(myWeakMap);//[null:1, null:2, null:3, null:4]
    }


    static class MyWeakMap {

        Entry[] table = new Entry[4];

        public void put(int index, String key, String value) {
            table[index] = new Entry(key, value);
        }

        public String get(String key) {
            for (Entry entry : table) {
                if (entry != null) {
                    String k = entry.get();
                    if (null != k && k.equals(key)) {
                        return entry.value;
                    }
                }
            }
            return null;
        }

        @Override
        public String toString() {
            return Arrays.toString(table);
        }

        static class Entry extends WeakReference<String> {

            String value;

            public Entry(String referent, String value) {
                super(referent);
                this.value = value;
            }

            @Override
            public String toString() {
                return get() + ":" + value;
            }
        }
    }


}

针对以上的场景我们可以通过ReferenceQueue可以监测到内存的泄漏,
在手动GC回收之后我们再次手动clean了一次,
在clean方法里通过queue去查找未被回收的对象,对里面的对象进行置空 释放内存。

package com.jalen.android.memoryLeak;

import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.util.Arrays;

/**
 * 测试弱引用的内存泄漏 参考ThreadLocalMap
 */
public class TestWeakReferenceQueue {


    public static void main(String[] args) {
        MyWeakMap myWeakMap = new MyWeakMap();
        myWeakMap.put(0, new String("a"), "1");
        myWeakMap.put(1, new String("b"), "2");
        myWeakMap.put(2, new String("c"), "3");
        myWeakMap.put(3, new String("d"), "4");
        System.out.println(myWeakMap);//[a:1, b:2, c:3, d:4]
        System.gc();
        System.out.println("手动GC之后");
        System.out.println(myWeakMap.get("a"));//null
        System.out.println(myWeakMap.get("b"));//null
        System.out.println(myWeakMap.get("c"));//null
        System.out.println(myWeakMap.get("d"));//null
        System.out.println(myWeakMap);//[null:1, null:2, null:3, null:4]
        System.out.println("手动clean之前");
        myWeakMap.clean();
        System.out.println("手动clean之后");//[null, null, null, null]
        System.out.println(myWeakMap);
    }


    static class MyWeakMap {
        static ReferenceQueue queue = new ReferenceQueue();
        Entry[] table = new Entry[4];

        public void put(int index, String key, String value) {
            table[index] = new Entry(key, value);
        }


        public void clean() {
            Object ref;
            while ((ref = queue.poll()) != null) {
                System.out.println(ref);
                for (int i = 0; i < table.length; i++) {
                    if (table[i] == ref) {
                        table[i] = null;
                    }
                }
            }
        }


        public String get(String key) {
            for (Entry entry : table) {
                if (entry != null) {
                    String k = entry.get();
                    if (null != k && k.equals(key)) {
                        return entry.value;
                    }
                }
            }
            return null;
        }

        @Override
        public String toString() {
            return Arrays.toString(table);
        }

        static class Entry extends WeakReference<String> {

            String value;

            public Entry(String referent, String value) {
                super(referent, queue);
                this.value = value;
            }

            @Override
            public String toString() {
                return get() + ":" + value;
            }
        }
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值