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

标签 java garbage-collection

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/

相关文章:

java - Multi-Tenancy 数据源之间的动态切换不起作用

c# - 如何在C#/MVVM应用程序中解决无法解释的ObjectDisposedExceptions?

java - 如何在 Java 8 中设置最小元空间

java - JSTL forEach 在 java 代码中使用变量

java - 如何让我的重置按钮起作用

java - 为垃圾收集清除 BST 的最佳方法是什么?

java - 8拼图java.lang.OutOfMemoryError : GC overhead limit exceeded

java - 如何诊断无休止的 JVM GC 运行?

java - 如何以编程方式启动具有多个配置文件的jetty服务器?

java - 如何使用 postgresql 在 Spring boot 中的 addScaler() 方法中添加枚举值?