Ehcache 的文档指出堆层受 Java 垃圾收集的约束(与堆外层和磁盘存储相反)。
那么,这是否意味着 Heap 层中的对象可以被 GC 自发删除?显然,当它们过期或空间不足时,它们会被 Ehcache 删除——这是一种明确定义的行为。但最重要的是,GC 能否就这样随机杀死一些对象,甚至不将它们移到较低层?
最佳答案
GC 不会收集 Activity 对象,即 is reachable from a live thread 的对象.堆上 Ehcache 存储中的对象是可访问的,因此不会被收集。
Ehcache 曾经尝试使用 WeakReference
但 according to this post这个想法被放弃了:
I thought this was a cool idea. In production our caches ended up looking like Swiss cheese as elements randomly disappeared. I was hoping the VM would keep all elements and only start discarding before running out of memory. Not so. It was removed about 8 months ago although I noted a few references in the java doc today.
关于java - Java 垃圾收集器可以随机删除堆上层中的对象吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53116637/