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

  • Home
  • SEARCH
  • 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 9196635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:53:37+00:00 2026-06-17T21:53:37+00:00

Short version: If you call string.substring(n,m).intern(), does the string table retain the substring or

  • 0

Short version: If you call string.substring(n,m).intern(), does the string table retain the substring or the original string?

…But I’m not sure that’s the right question to ask, so here’s the long version:

I’m working with legacy Java code (PCGen) that parses files by slurping each in as one big string and then using String.split, .trim, .substring, and StringTokenizer to decompose them into tokens. This is very efficient for parsing, because none of those methods copy the original string, but all point at parts of a shared char[].

After parsing is over, I want to reclaim some memory. Only a few small substrings of the original big string are needed, but the strong reference keeps the big string from being collected. And later I’m suffering OOM, I believe due in part to that huge heap impact of lots of parsed files.

I know I can trim the big string down via new String(String)(copy-on-write). And I know I can reduce string duplication via String.intern (which is important because there’s a lot of redundancy in the parsed files). Do I need to use both to reclaim the greatest quantity of heap, or does .intern() do both? Reading the OpenJDK7 hotspot source code (hotspot/src/share/vm/classfile/symbolTable.cpp) it looks like the string table keeps the whole string and does not trim it for offset/length at all. So I think I need to make a new String and then intern that result. Right?

All that said, switching to a streaming parser would be a big win in terms of memory, but that’s too big a change for the short term.

  • 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-06-17T21:53:38+00:00Added an answer on June 17, 2026 at 9:53 pm

    You can use new String(String) and the intern() method and this will take a copy as required for up to Java 7 update 4. From Java 7 update 5 substring will take a deeper copy, but you may still want to use intern(). Note: Java 7 uses the heap, not the perm gen to store String literals.

    public static void main(String[] args) {
        char[] chars = new char[128];
        Arrays.fill(chars, 'A');
        String a128 = new String(chars);
        printValueFor("a128", a128);
        String a16 = a128.substring(0, 16);
        printValueFor("a16", a16);
    }
    
    public static void printValueFor(String desc, String s) {
        try {
            Field value = String.class.getDeclaredField("value");
            value.setAccessible(true);
            char[] valueArr = (char[]) value.get(s);
            System.out.println(desc + ": " + Integer.toHexString(System.identityHashCode(valueArr)) + ", len=" + valueArr.length);
        } catch (Exception e) {
            throw new AssertionError(e);
        }
    }
    

    on Java 7 update 4 prints

    a128: 513e86ec, len=128
    a16: 53281264, len=16
    

    I would expect that Java 6 does not do this.

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

Sidebar

Related Questions

Short version: How can I map two columns from table A and B if
Short and sweet version: Is there a single web service method that would return
Short version: I'm extending a jQuery object function that is intended to take 0-3
Short version: Can a COM class modify its own virtual table entries at runtime?
Short version: Can I grant access to external databases to a role? Long version:
Short version : I would like the maven-glassfish-plugin to only be executed in the
Short Version: I'm looking for a technique to keep nearly-sorted data in nearly-sorted order
Short version: In pure Java EE 6, is there something like Spring's Authentication Processing
Short version : echo testing | vim - | grep good This doesn't work
Short version: I have a similar setup to StackOverflow. Users get Achievements. I have

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.