【发布时间】:2019-04-05 15:12:21
【问题描述】:
使用JDK 1.8和VisualVM,我看到一个字符串的保留大小是0。根据this article,这意味着分配给该字符串的内存为0。这是否意味着该字符串已经被GC?如果如果已经是 GC,为什么它仍然显示?如果不是,retained size=0 是什么意思?是不是表示“如果 JVM GC 这个字符串,它只能获得 0 KB 的可用内存”?
示例代码为:
public class Main {
private static final Logger LOGGER = Logger.getLogger(Main.class.getName());
private static ExecutorService executorService = Executors.newFixedThreadPool(3);
public static void main(String[] args) throws InterruptedException {
AAAAAAAA a = new AAAAAAAA();
a.setString();
Thread.sleep(55555555); // I dump it when it's asleep.
}
}
class AAAAAAAA {
String string = "wawawawa";
public void setString() {
string = "hahahahaha";
}
}
【问题讨论】: