0

Possible Duplicate:
how many characters can a Java string have?

How many characters can a string hold at it's maximum. (java)

Community
  • 1
  • 1
Demian Kasier
  • 2,475
  • 6
  • 30
  • 40

4 Answers4

3

heap memory is the limit OR the Integer.MAX_VALUE which ever is smaller.

jmj
  • 237,923
  • 42
  • 401
  • 438
  • 2
    Not necessarily. I don't know how well various JVMs cope with huge objects (a `char[Integer.MAX_VALUE]` for example) but it's certainly possible to run a JVM with more than that much heap memory... but you'd still be limited in terms of the size of a string. – Jon Skeet Dec 08 '10 at 08:39
  • @Jon Skeet I didn't get your point how jvm will work on objects which are larger than heap memopry I mean where it will store it ? – jmj Dec 08 '10 at 08:41
  • My point wasn't about objects larger than heap memory - it was a case of wondering whether the JVM can cope with a single object being nearly 4GB. I know the CLR has (or had) a 1GB-per-object limit, for example. You can use more than 4GB in total in a JVM (if you have the memory) but you can't necessarily create a single object which is that big. – Jon Skeet Dec 08 '10 at 08:46
  • @Jon Skeet Totally agree with your point – jmj Dec 08 '10 at 08:49
  • life.java: You can request memory directly from the operating system. Some memory managers do that for objects which are significantly larger then a memory page. The advantage is that the memory can be returned to the OS after usage. Because of fragmentations heap memory can usually not be returned to the OS — But I never heard of a JVM doing that. – Martin Dec 08 '10 at 08:52
0

A lot. Specifically, there does not seem to be any limit, though you will eventually out-of-memory. See the JVM spec for more info, or lack of info: http://java.sun.com/docs/books/jvms/second_edition/html/Concepts.doc.html#25486

EDIT: It looks like Java (the language) allows you to to make a string the length of any single variable, which is probably the amount of heapspace Java has allocated. (see the -Xmx argument).

Robert
  • 6,412
  • 3
  • 24
  • 26
0

In theory: 2^31 - 1 = 2147483647 (~2 GigaByte). In practice: end of virtual memory.

0

Since String is based on an underlying char[], and array indices are int values, the maximum length of a String is Integer.MAX_VALUE, though you'll probably run into memory issues before that.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720