Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5846013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:31:01+00:00 2026-05-22T12:31:01+00:00

I found that using String.substring is known for memory issues related to String.split .

  • 0

I found that using String.substring is known for memory issues related to String.split.

Is there a memory leak in using String.split?

If yes what is the work-around for it?


Following link show correct usage of substring in Java.

https://bugs.java.com/bugdatabase/view_bug?bug_id=4513622


One more blog which talk about possible MLK in substring.

http://nflath.com/2009/07/the-dangers-of-stringsubstring/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:31:02+00:00Added an answer on May 22, 2026 at 12:31 pm

    Update: Behavior has changed in 1.7.0_06: See this article: Changes to String internal representation made in Java 1.7.0_06 at java-performance.info.


    As pointed out by @finnw there is indeed kind of a memory leak lurking around when using String.substring. The reason is that String.substring only returns a view of a portion of the given string, i.e., the underlying string is still kept in memory.

    To force the creation of a new string, unrelated to the source, you’ll have to use the new keyword. I.e., you’ll have to do for instance

    String[] parts = orig.split(";");
    //String mySubstring = parts[i];               // keeps orig from being GC'd
    String mySubstring = new String(parts[i]);     // creates a new string.
    

    or, perhaps more direct

    String mySubstring = new String(orig.split(";")[i]);
    

    I must say that this behavior seems “unnecessary” to me. It should be solvable using weak references or some other technique. (Especially considering that String is already a special class, part of the Java language specification.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using a RichTextBox in WinForms 3.5 and I found that when I programmatically
I'm using code that I found on the CodeProject.com for a low-level keyboard hook
I've found a couple of references ( for example ) that suggest using final
The only reliable method that I a have found for using a script to
I have found that there is generally a singe type or namespace that takes
Last night I was messing around with Piglatin using Arrays and found out I
Using C, I need to find a substring inside a buffer that may contain
We’ve found that the unit tests we’ve written for our C#/C++ code have really
I've found that on some occasions I can edit the source while debugging. Are
I have found that my HTML is, to be honest, very clunky. Small, simple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.