简体   繁体   English

Java垃圾收集器

[英]Java garbage collector

IS there a way to set a garbage collector only for one type, like in Objective C when you do [Oppsie release]; 有没有办法只为一种类型设置一个垃圾收集器,比如在你做[Oppsie release];时在Objective C中[Oppsie release]; Or is there a similar way in Java. 或者Java中是否有类似的方式。 I was thinking for setting Oppsie to null but I don't think it's similar to release. 我正在考虑将Oppsie设置为null但我不认为它与发布类似。

Any suggestions? 有什么建议么?

Once you set Oppsie to null and is no longer reachable by any other reference, Java will set it as unreachable and mark it for garbage collection. 将Oppsie设置为null并且任何其他引用都无法访问它之后,Java会将其设置为无法访问并将其标记为垃圾回收。 You will no longer be able to reference it and Java will eventually GC it. 您将无法再引用它,Java将最终GC它。

If this is a Java question then the answer is: DO NOT WORRY ABOUT GARBAGE COLLECTION! 如果这是一个Java问题,那么答案是:不要担心垃圾收集!

Make sure that Objects that you no longer need are not referenced by any live variable and you're good! 确保您不再需要的对象不被任何实时变量引用,并且您很好! If you get any GC related exception 99% of the time you have the chance to fix it by optimizing your code. 如果您在99%的时间内通过优化代码获得GC相关异常,则有机会通过优化代码来解决问题。 Again, don't invoke garbage collection and don't worry about that. 同样,不要调用垃圾收集,也不要担心。

I am not sure what you mean by setting a garbage collector to null only for one type. 我不确定你的意思是将垃圾收集器设置为仅为一种类型的null。 What you are suggestion (by setting Oppsie to null) is setting an INSTANCE to null, nothing to do with types... 您的建议是什么(通过将Oppsie设置为null)将INSTANCE设置为null,与类型无关......

The short answer is No anyway, you cant set the garbage collector for "one type" if you mean a class or something like that by it. 简短的回答是无论如何,你不能将垃圾收集器设置为“一种类型”,如果你的意思是一个类或类似的东西。 Once an object has no references made to it it'll be automatically marked for GC. 一旦对象没有对其进行引用,它将自动标记为GC。 You can suggest to java to trigger GC, but it is not guaranteed to happen. 您可以建议java触发GC,但不保证会发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM