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 9273507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:08:03+00:00 2026-06-18T16:08:03+00:00

I have android application which takes huge String Object from telnet client. Later I

  • 0

I have android application which takes huge String Object from telnet client. Later I Use only small part of the big String. I use

new String(Part of old string);

To separate new string Char array from old Strings char array. So the old String Should get garbage collected, but new string surprisingly still has an reference to the old object.
I can see it with “Eclipse Memory Analyzer”.
And that overflows my tiny 16Meg application memory quickly.

How to avoid that situation?

    private WifiChannel parse1(String channLine){
    //scanning with "iwlist wlan0 scanning" the getChans1 method
    String[] input = channLine.split(System.getProperty("line.separator"));
    if (input.length < 4);
    String segment[];
    String segment2[];
    WifiChannel chan = new WifiChannel();
    try {
        if (input.length > 5){
            chan.setMacAddress(new String(input[0]));
            segment = input[1].split(":");
            chan.setChannel(Integer.parseInt(segment[1].trim()));
            segment = input[3].split(" ");
            segment2 = segment[20].split("=");
            chan.setQuality(new String(segment2[1]));
            segment2 = segment2[1].split("/");
            chan.setSignalStrength((Integer.parseInt(segment2[0].trim())*100)/Integer.parseInt(segment2[1].trim())+"%");
            segment2 = segment[23].split("=");
            try{chan.setSignalLevel(Integer.parseInt(segment2[1].trim()));}catch(Exception e){chan.setSignalLevel(0);}
            segment = input[5].split(":");
            chan.setName(new String(segment[1].replaceAll("^\"|\"$", "")));
            for (int i = 6;i<input.length;i++)
                if (input[i].contains("Mode"))
                    segment = input[i].split(":");
            chan.setChannelMode(new String(segment[1]));
            String band = "";
            if(channLine.contains("5.5 Mb/s"))band = band +"b";
            if(channLine.contains("12 Mb/s") )band = band +"g";
            chan.setBand(new String(band));
        }
    }catch (Exception e){Log.e("","",e);}
    return chan;
}

Method input is also part of bigger String.

  • 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-18T16:08:05+00:00Added an answer on June 18, 2026 at 4:08 pm

    Every String instance is backed by a char array:

    public class String {
    
        private final char[] value;
    
        ...
    }
    

    Because of efficiency reasons, some operations on a string can can create a new string instance that shares the char[] with the original string. This is possible because all strings are immutable. An example of this is the substring() method:

    public String substring(int start) {
        if (start == 0) {
            return this;
        }
        if (start >= 0 && start <= count) {
            return new String(offset + start, count - start, value); // !!!
        }
        throw indexAndLength(start);
    }
    

    The line with my comment added calls a constructor that doesn’t create a copy of the char[], but directly references it. You can’t call this constructor directly, because it is package-private and therefore it is used only internally by methods such as substring:

    /*
     * Internal version of the String(char[], int, int) constructor.
     * Does not range check, null check, or copy the character array.
     */
    String(int offset, int charCount, char[] chars) {
        this.value = chars;
        this.offset = offset;
        this.count = charCount;
    }
    

    Now, if you create a short substring from a very long string, then the short one still references the original large char[]. Even after the original string is garbage collected, its large array still stays in the memory, even though only only a small portion of it is now accessible by the new substring. This effectively creates a memory leak.

    To fix this problem, the usual technique is to create a new string from the substring using the copy constructor, which makes a copy of only the needed range of the original char[].

    String longString = "1234567890";
    // backed by char[] of length 10
    
    String substring = longString.substring(5);
    // value is "67890", but still backed by the original char[] of length 10
    
    String copy = new String(substring);
    // also has value "67890", but now backed only by char[] of length 5
    

    Edit:

    For completeness, this is the source of the copy-contructor. As you can see, if the original string references an array of the same length as of the string itself, then the array doesn’t need to be copied, because there are no “dead characters” in it. However, if the array is larger then the length of the string, then a copy of the “live” range of the array is performed.

    public String(String toCopy) {
        value = (toCopy.value.length == toCopy.count)
            ? toCopy.value
            : Arrays.copyOfRange(toCopy.value, toCopy.offset,
                toCopy.offset + toCopy.length());
        offset = 0;
        count = value.length;
    }
    

    Note:
    All the above source code comes from Android API 15

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

Sidebar

Related Questions

I am developing an android application in which I have passed a string from
1.I have an android client application in which i am grabbing frames from the
i have created an android application which takes the input qrcode image from it
I am developing android application in which i have to open option menu from
I have an Android 13mb application, which displays offline/cached map from sd card. I
Hello I was just writing a little Android application which takes an image from
I have a Android Application which is basically uses WebView for all interaction.. How
I have an android application in which I have to execute some tasks at
i'm building an android application which have a chat. in this chat i each
I have a problem with my android application. That application which uses the kSOAP

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.