gpt4 book ai didi

java - GC后,对象在内存中的地址变了,为什么对象引用仍然有效?

转载 作者:搜寻专家 更新时间:2023-11-01 01:54:09 25 4
gpt4 key购买 nike

Java objects are created in Heap and Heap is divided into three parts or generations for sake of garbage collection in Java, these are called as Young generation, Tenured or Old Generation and Perm Area of heap. New Generation is further divided into three parts known as Eden space, Survivor 1 and Survivor 2 space. When an object first created in heap its gets created in new generation inside Eden space and after subsequent Minor Garbage collection if object survives its gets moved to survivor 1 and then Survivor 2 before Major Garbage collection moved that object to Old or tenured generation.

阅读更多:http://javarevisited.blogspot.com/2011/04/garbage-collection-in-java.html#ixzz2MeKK2gBA

所以我的问题是,在这些移动操作之后,内存中的地址应该更改,为什么对象引用仍然有效?

最佳答案

如果 GC 决定移动一个对象,它有责任更新对该对象的所有引用。

这对 Java 程序员来说是透明的:他们可以将引用视为抽象句柄,而不用担心 JVM 如何管理对象存储。

关于java - GC后,对象在内存中的地址变了,为什么对象引用仍然有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15218438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com