字符串和垃圾收集字符串、垃圾

2023-09-02 23:53:20 作者:忘了怎么哭

我听到相互矛盾的故事,关于这个话题,我期待为清楚一点。

I have heard conflicting stories on this topic and am looking for a little bit of clarity.

将如何处置一个字符串对象立即或在它最起码痕迹明显?

How would one dispose of a string object immediately, or at the very least clear traces of it?

推荐答案

这要看情况。文字字符串是拘留每默认,所以即使你的应用程序不再引用它它不会被收集,因为它是由内部实习结构引用。其他的字符串是就像任何其他管理对象。只要他们是由您的应用程序不再引用他们有资格进行垃圾回收。

That depends. Literal strings are interned per default, so even if you application no longer references it it will not be collected, as it is referenced by the internal interning structure. Other strings are just like any other managed object. As soon as they are no longer reference by your application they are eligible for garbage collection.

更多关于实习在这里这个问题:http://stackoverflow.com/questions/372547/where-do-java-and-net-string-literals-reside/372559#372559

More about interning here in this question: http://stackoverflow.com/questions/372547/where-do-java-and-net-string-literals-reside/372559#372559

 
精彩推荐
图片推荐