本文介绍了另一个字符串的子字符串是否会阻止父字符串被垃圾收集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  String samplel =ToBeGarbageCollected; 
String sample2 = samplel.substring(0,1);
samplel = null;

我知道substring在内部会保留原始String的引用。



但是通过明确定义 samplel null ,will sample1和sample2可用于垃圾回收?



我记得在某处如果父对象被明确设置为 null 所有子值都可用于垃圾收集。这会对上述情况有好处吗?
我只是好奇,如果这是父母子女的关系情况?如果没有,这会导致 sample1 sample2 可用于垃圾收集?

  String samplel =ToBeGarbageCollected; 
String sample2 = new String(samplel .substring(0,1));
samplel = null;


解决方案

首先要说的是垃圾收集不会立即发生。因此,将 null 分配给任何内容不会导致垃圾回收。 可能做的是导致对象无法访问 ...,这将使它成为未来GC运行中垃圾收集的潜在候选者。






现在来看你的具体例子。



重要说明:以下仅适用于较旧的JVM;即Java 7 update 5及更早的版本。在Java 7 update 6中,他们更改了 String.substring(),以便目标字符串和生成的子字符串不共享支持数组。这消除了 substring 潜在的存储泄漏问题。






substring 方法不会在新字符串中引用原始字符串。它实际上做的是保存对原始String的后备数组的引用;也就是说包含字符的数组。



但是已经说过,将 null 赋值给 samplel 不足以使整个原始字符串的状态无法访问。原始字符串的整个支持数组将保持可到达......这意味着它不会成为垃圾收集的候选人。



但是还有另一个复杂因素。您将 sample1 设置为一个String文字,并且表示String文字的String对象始终可达(除非整个类被卸载!)

答案与上述情况相同,其中 sample1 是一个文字。



如果 sample1 不是文字,并且没有其他参考然后 sample1 ,临时子字符串将不可用。






理论上它会。

实际上,它取决于GC最终是否可以查找时仍然可以访问引用......以及所讨论的字符串是否足够大并且数量很多足以对内存使用产生重大影响。



实际上,前提是通常不满足,并创建一个新的字符串不起作用。

String samplel = "ToBeGarbageCollected";
String sample2 = samplel.substring(0, 1);
samplel = null;

I know substring internally will keep a reference for original String.

But by explicitly defining samplel as null, will sample1 and sample2 be available for garbage Collection?

I remember seeing somewhere if a parent object is explicitly set to null all child values are available for garbage collection. Will this hold good for the above?I am just curious if this the parent child relationship scenario? If not, will this cause sample1 or sample2 to be available for garbage collection?

String samplel = "ToBeGarbageCollected";
String sample2 = new String(samplel .substring(0, 1));
samplel  = null;
解决方案

First thing to say is that garbage collection doesn't happen immediately. So assigning null to anything does not / cannot cause garbage collection. What is may do is to cause an object to become unreachable ... and that will make it a potential candidate for garbage collection in a future GC run.


Now to your specific examples.

Important Note: the following only applies to older JVMs; i.e. Java 7 update 5 and earlier. In Java 7 update 6, they changed String.substring() so that the target string and resulting substring DO NOT share the backing array. This eliminates the potential storage leak issue with substring.


The substring method doesn't put a reference to the original String in the new String. What it actually does is save a reference to the original String's backing array; i.e the array that holds the characters.

But having said that, assigning null to samplel is not sufficient to make the state of the entire original string unreachable. The original String's entire backing array will be remain reachable ... and that means it won't be a candidate for garbage collection.

But there is another complication. You set sample1 to a String literal, and the String object that represents a String literal is always reachable (unless the entire class gets unloaded!)

The original sample1 object will remain fully reachable, and sample2 will remain be reachable unless that variable goes out of scope.

If sample1 had not been a literal and there were no other references to it, then the answer would be different. The sample1 object would be unreachable, but its backing array would still be reachable via sample2.


In your second example, copying the substring causes a new String to be created. And it is guaranteed that the new String won't share the backing array with the original String and the temporary substring. In that case, assigning null is unnecessary.

The answer is the same as for the above for the case where sample1 is a literal.

If sample1 is not a literal and there are no other references to it, then sample1 and the temporary substring would now be unreachable.


In theory it will be.

In practice it depends on whether the references are still reachable when the GC eventually gets around to looking ... and also on whether the Strings in question are large enough and numerous enough to have a significant impact on memory usage.

And in practice, that precondition is usually NOT satisfied and creating a fresh String like that usually does NOT help.

这篇关于另一个字符串的子字符串是否会阻止父字符串被垃圾收集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 06:51