据我所知,对于旧一代JVM中的空间,它可以用于两个目的,
我的问题是,
答案 0 :(得分:1)
让我回答2.它绝对不是一个深层副本:GC将对象作为不同的内存块处理,深度复制实际上只意味着复制连接到对象图中的许多不同对象。此外,想象一下移动,而不是副本,你会得到更好的服务;复制实际上只是一个“实现细节”,所需的效果是重新定位。
答案 1 :(得分:1)
放置此代码以回应@Lin Ma在其评论中提出的问题:
class SomeClass{
private static List<Object> memoryLeakCulprit = new ArrayList<Object>();
void someMethod(Object obj){
//adding the reference of some object in the culprit list
memoryLeakCulprit.add(obj);
}
//supposed to remove the reference passed
void someOtherMethod(Object obj){
obj = null;
//bummer forgot to remove the reference from list
//now the instance is not reachable by obj
//so now as new references are added to culprit list the memory will keep on increasing in size
}
}
<强> UDPATE 强>
如何解决此漏洞
oid someOtherMethod(Object obj){
//before setting the obj reference to null it must be removed from culprit list
memoryLeakCulprit.remove(obj);
//now its safe to set this reference to null
obj = null;
}
使用某些分析工具(例如 JProfiler , VisualVM )解决将其泄漏到配置文件应用程序的唯一方法,并找出哪个类是导致泄漏。
当你找到课程时,代码将被改变,这是唯一的方法。
在程序退出之前,无需释放参考文献。原因是static
变量(memoryLeakCulprit
)绑定到 Class 对象,并且一旦退出程序,所有引用都会自动释放,包括类对象。
另外,请务必在退出程序之前关闭系统资源(套接字,数据库连接)。