Java String.substring method potential memory leak?

前端 未结 3 1458
灰色年华
灰色年华 2020-11-29 07:49

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.

3条回答
  •  一生所求
    2020-11-29 08:23

    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).

    Note that this has changed since Java 7u6. See http://bugs.sun.com/view_bug.do?bug_id=4513622.

    The original assumptions around the String object implementing a flyweight pattern are no longer regarded as valid.

    See this answer for more info.

提交回复
热议问题