本文介绍了Java String.substring方法潜在的内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在浏览String类API,看起来像substring方法导致潜在的内存泄漏,因为它与原始String共享相同的字符数组。I was going through the String class API and looks like there is a potential memory leak caused by substring method as it shares same character array as original String.如果原始字符串很大,则substring返回的小字符串可以防止原始字符串(由大数组备份)从Java中的垃圾收集。If original string is huge then small string returned by substring can prevent original string(backed up by large array) from garbage collection in Java.任何想法或我读错了API。Any thoughts or did I read the API wrong.推荐答案 是 String(String)构造函数),则可能发生内存泄漏。There is a potential for a memory leak, if you take a substring of a sizable string and not make a copy (usually via the String(String) constructor).请注意,自 Java 7u6 。 请参阅 http://bugs.sun.com/view_bug.do?bug_id = 4513622 。实现 String 对象周围的原始假设://en.wikipedia.org/wiki/Flyweight_patternrel =nofollow noreferrer> flyweight模式不再被视为有效。The original assumptions around the String object implementing a flyweight pattern are no longer regarded as valid.参见这个答案了解更多信息。 这篇关于Java String.substring方法潜在的内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-20 21:51