This discussion is archived
11 Replies Latest reply: Mar 22, 2010 5:02 AM by EJP RSS

Memory consumption: String vs char array

807580 Newbie
Currently Being Moderated
I have a java application which in Jprofiler shows tens of thousands of strings created. We are using strings in our application and frequently re-initialize them, but not to the extent that the count moves up to ten of thousands. Strings are immutable structures. Is it that the strings shown in JProfiler are the ones that are being created but not cleaned up?

Should we use charArrays instead? They are mutable.

Vivek
  • 1. Re: Memory consumption: String vs char array
    Kayaman Guru
    Currently Being Moderated
    Short answer no with a but, long answer yes with an if.

    Does it make sense to use char arrays? I.e. are you currently handling the Strings like they were an array of characters, instead of words or sentences or paragraphs.

    If you're doing a lot of character twiddling, you might want to use StringBuilders instead of Strings.


    Even if JProfiler shows you have thousands of Strings, you probably shouldn't worry about it unless they're not being reclaimed and you're running out of memory.
  • 2. Re: Memory consumption: String vs char array
    gimbal2 Guru
    Currently Being Moderated
    in the lifetime of the average application many strings will be created and subsequently thrown away, yes. It is expected behavior and the JVM will do all it can to optimize this as far as possible (for example, by using string pooling). I wouldn't worry about that too much unless your profiler can show you a spot where these string manipulations are creating a dent in performance.
  • 3. Re: Memory consumption: String vs char array
    807580 Newbie
    Currently Being Moderated
    Kayaman wrote:
    If you're doing a lot of character twiddling, you might want to use StringBuilders instead of Strings.
    I see your point here. So indirectly you suggest to use StringBuilder instead of string. But when I need to compare the text with other literal strings, it would again require to be converted to a string. What I think is, won't it be better to take char arrays and use standard stringCompare functions to do a char-by-char comparison? In short, what JVM does for me in case of strings, I would do it myself, but at the cost of a mutable structure.
    Kayaman wrote:
    Even if JProfiler shows you have thousands of Strings, you probably shouldn't worry about it unless they're not being reclaimed and you're running out of memory.
    Actually my RAM is limited to 1 GB and my application is consuming 690 MB of memory. That's what bothers me. In that case, I may have to run the Garbage collector myself.

    Vivek
  • 4. Re: Memory consumption: String vs char array
    807580 Newbie
    Currently Being Moderated
    gimbal2 wrote:
    I wouldn't worry about that too much unless your profiler can show you a spot where these string manipulations are creating a dent in performance.
    Thats the exact problem, where I cannot use JProfiler to see the precise location where these strings are created. As of now, it shows me the application's memory snapshot where strings are counting in thousands.

    Vivek
  • 5. Re: Memory consumption: String vs char array
    798906 Newbie
    Currently Being Moderated
    You will find that interned strings go into the "permanent generation". I don't think the concurrent collector (CMS), by default, collects the permanent generation. I'm not sure about the other collectors so you may want to check. If you are using the concurrent collector you can turn on -XX:+CMSPermGenSweepingEnabled -XX:+CMSClassUnloadingEnabled, although interned strings could only be collected when they become unreferenced of course (whether they actually are or not would depend on the implementation I guess).

    On memory consumption - a string does have an overhead compared to a char[], a string with 0 characters takes 40 bytes. If you have thousands and thousands of very short strings then there would be a noticeable memory overhead compared to using char[]. The longer the strings the less the overhead as a percentage of the total size.
  • 6. Re: Memory consumption: String vs char array
    807580 Newbie
    Currently Being Moderated
    On memory consumption - a string does have an overhead compared to a char[], a string with 0 characters takes 40 bytes.
    A zero-sized char array also takes some place.
  • 7. Re: Memory consumption: String vs char array
    798906 Newbie
    Currently Being Moderated
    BIJ001 wrote:
    On memory consumption - a string does have an overhead compared to a char[], a string with 0 characters takes 40 bytes.
    A zero-sized char array also takes some place.
    Yes, 16 bytes
  • 8. Re: Memory consumption: String vs char array
    EJP Guru
    Currently Being Moderated
    I have a java application which in Jprofiler shows tens of thousands of strings created.
    That is entirely normal. Last time I did an exercise like that I found the two most populous classes were String and AWTEventMulticaster (it was a GUI program), by a considerable margin.

    I wouldn't worry about it.
  • 9. Re: Memory consumption: String vs char array
    807580 Newbie
    Currently Being Moderated
    BIJ001 wrote:
    A zero-sized char array also takes some place.
    Yes. But string is immutable, whereas char array is mutable.
    // Code-1
    String str = null;
    str = "";
    ....
    str = "";
    
    // Code-2
    char[] arrChar = new char[1];
    arrChar[0] = '\0';
    ....
    arrChar[0] = '\0';
    By end of Code-1, 80 bytes of memory is blocked, out of which 40 bytes (of first initialization of str) would be taken up by garbage collector in its next cleanup cycle.

    By the end of Code-2, only 16 bytes of memory is blocked. Please correct me if wrong.

    Vivek
  • 10. Re: Memory consumption: String vs char array
    EJP Guru
    Currently Being Moderated
    Yes. But string is immutable, whereas char array is mutable.
    So?
    String str = null;
    Takes just the space for the reference.
    str = "";
    Points the reference into the constant pool entry for "".
    str = "";
    Points the reference into the constant pool entry for "". Again.

    No garbage-collectable memory there whatsoever.
    char[] arrChar = new char[1];
    Takes up space for the reference and space for the array.
    arrChar[0] = '\0';
    Has no effect on memory usage.
    arrChar[0] = '\0';
    Has no effect on memory usage, again.
    By end of Code-1, 80 bytes of memory is blocked
    No.
    out of which 40 bytes (of first initialization of str) would be taken up by garbage collector in its next cleanup cycle.
    No. All the memory concerned apart from the reference itself is in the constant pool for the class, put there by the compiler, once per unique literal in the source text of the class. There is only one unique literal there and it is zero length. None of it is garbage-collectable.
  • 11. Re: Memory consumption: String vs char array
    807580 Newbie
    Currently Being Moderated
    Ok. Let me modify my example a bit:
    // Code-1
    String str = null;
    str = "abc";
    ....
    str = "pqr";
     
    // Code-2
    char[] arrChar = new char[1];
    arrChar[0] = 'a';
    ....
    arrChar[0] = 'b';
    Now does my analysis stand true?

    Vivek