问题描述
- android中GC后弱引用与JAVA中的不同点
-
JAVA和android中都执行以下代码,分别得到hello,null和hello,hello
WeakReference wr= new WeakReference(new String("hello"));
System.out.println(wr.get());
System.gc();
System.out.println(wr.get());有大神解答下吗?
解决方案
调用gc并不保证一定能马上回收对象,只是告诉系统需要回收,系统得到时间片才会去处理
解决方案二:
Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.
时间: 2024-12-23 21:49:08