I have a string that seems to be truncated at around 900 chars (due to
how the output is used it is hard to tell exactly how many [println a
length would break half a dozen programs])... if this a real limit or
dis something weird just happen...
platform: jdk1.6 (native) on FreeBSD 7.2-RELEASE pl2 (i386)
Sabine Dinis Blochberger - 27 Jul 2009 09:33 GMT
> I have a string that seems to be truncated at around 900 chars (due to
> how the output is used it is hard to tell exactly how many [println a
> length would break half a dozen programs])... if this a real limit or
> dis something weird just happen...
>
> platform: jdk1.6 (native) on FreeBSD 7.2-RELEASE pl2 (i386)
Here's a discussion on the subject:
<http://www.velocityreviews.com/forums/t141739-max-length-of-a-string.html>
In your case, it's probably not enough available memory, or some bug in
your code.

Signature
Op3racional - www.op3racional.eu
---------------------
If you're reading this, you're on Usenet
<http://oakroadsystems.com/genl/unice.htm>
Roedy Green - 27 Jul 2009 14:11 GMT
On Mon, 27 Jul 2009 00:44:07 -0700 (PDT), "Aryeh M. Friedman"
<Aryeh.Friedman@gmail.com> wrote, quoted or indirectly quoted someone
who said :
>I have a string that seems to be truncated at around 900 chars (due to
>how the output is used it is hard to tell exactly how many [println a
>length would break half a dozen programs])... if this a real limit or
>dis something weird just happen...
Strings are just char[] internally. So the limit is 32k elements, but
on a 32 bit JVM you will run out of RAM first.
I suggest you pepper your code with asserts or prints of the string
length to figure out where it is being chopped.
There may be some limit imposed by your OS on console I/O. Try
writing to a file.
see http://mindprod.com/applet/fileio.html
for how to write the code.

Signature
Roedy Green Canadian Mind Products
http://mindprod.com
"The industrial civilisation is based on the consumption of energy resources that are inherently limited in quantity, and that are about to become scarce. When they do, competition for what remains will trigger dramatic economic and geopolitical events; in the end, it may be impossible for even a single nation to sustain industrialism as we have know it in the twentieth century."
~ Richard Heinberg, The Partys Over: Oil, War, and the Fate of Industrial Societies
Mike Amling - 27 Jul 2009 17:15 GMT
> On Mon, 27 Jul 2009 00:44:07 -0700 (PDT), "Aryeh M. Friedman"
> <Aryeh.Friedman@gmail.com> wrote, quoted or indirectly quoted someone
[quoted text clipped - 7 lines]
> Strings are just char[] internally. So the limit is 32k elements, but
> on a 32 bit JVM you will run out of RAM first.
32k elements? Arrays in Java are limited to 2G elements, 2**31-1 to
be precise.
--Mike Amling
Roedy Green - 27 Jul 2009 23:02 GMT
> 32k elements? Arrays in Java are limited to 2G elements, 2**31-1 to
>be precise.
Oops. Showing my age. That's for DEC PDP 11 arrays. It seems amazing
you could do anything back then with such tight constraints.

Signature
Roedy Green Canadian Mind Products
http://mindprod.com
The USA and Iraq are as mismatched combatants as Mike Tyson and a bag lady. Americans make themselves look ridiculous when they justify invading and occupying Iraq in self defence.
Mike Amling - 28 Jul 2009 04:18 GMT
>> 32k elements? Arrays in Java are limited to 2G elements, 2**31-1 to
>> be precise.
>
> Oops. Showing my age. That's for DEC PDP 11 arrays. It seems amazing
> you could do anything back then with such tight constraints.
I should have said 2**31 elements (with subscripts 0 .. 2**31-1).
--Mike Amling
Mike Schilling - 29 Jul 2009 00:10 GMT
>> 32k elements? Arrays in Java are limited to 2G elements, 2**31-1
>> to
[quoted text clipped - 3 lines]
> amazing
> you could do anything back then with such tight constraints.
Back when 4MB of RAM and 80MB of disk were luxuries. (Though I used
to work on a machine where the full 64K of RAM was too expensive, so
we settled for 56K, and it ran a refinery plant.)
Simon - 27 Jul 2009 14:21 GMT
> I have a string that seems to be truncated at around 900 chars (due to
> how the output is used it is hard to tell exactly how many [println a
> length would break half a dozen programs])... if this a real limit or
> dis something weird just happen...
>
> platform: jdk1.6 (native) on FreeBSD 7.2-RELEASE pl2 (i386)
Besides was has already been mentioned: Keep in mind that if longString
is a very long String, the following piece of code will not free any
(significant amount of) memory:
String shortString = longString.substring(offset, shortLength),
// then throw away all references to longString and have it gc'ed.
This is because shortString will still reference the same char[] array
that is also used by longString. Don't know whether this applies in your
case, but it is a common problem if you deal with long strings of which
you keep only small substrings. (At least this was the case in, I think,
Java 1.4. It may have been changed meanwhile, but I don't think so.)
Anyway I would expect Java to throw an exception rather than silently
truncating your string.
Cheers,
Simon
Roedy Green - 27 Jul 2009 23:04 GMT
> String shortString = longString.substring(offset, shortLength),
> // then throw away all references to longString and have it gc'ed.
>
>This is because shortString will still reference the same char[] array
>that is also used by longString.
in that case you can use:
String shortString = new String( longString.substring( offset,
shortLength));
to unpin the underlying long string and free it up for GC.
That is one of the few times you legitimately use new String.

Signature
Roedy Green Canadian Mind Products
http://mindprod.com
The USA and Iraq are as mismatched combatants as Mike Tyson and a bag lady. Americans make themselves look ridiculous when they justify invading and occupying Iraq in self defence.
Tom Anderson - 27 Jul 2009 14:32 GMT
> I have a string that seems to be truncated at around 900 chars (due to
> how the output is used it is hard to tell exactly how many [println a
> length would break half a dozen programs])... if this a real limit or
> dis something weird just happen...
It's something weird.
Try this:
public class StringLength {
public static void main(String... args) {
final int size = 1000;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < size; ++i) {
sb.append('!');
}
String s = sb.toString();
System.out.println(s.length());
}
}
I suspect that the string isn't really being truncated, but something
that's printing it is cutting it off at ~900 characters for some reason.
Tell us more about what's happening in your code, and we might be able to
suggest what the problem really is.
tom

Signature
I know thats not really relevant but I've just typed the words and my
backspace key doesn't work. -- phorenzik
Roedy Green - 27 Jul 2009 23:10 GMT
On Mon, 27 Jul 2009 14:32:03 +0100, Tom Anderson
<twic@urchin.earth.li> wrote, quoted or indirectly quoted someone who
said :
>I suspect that the string isn't really being truncated, but something
>that's printing it is cutting it off at ~900 characters for some reason.
>Tell us more about what's happening in your code, and we might be able to
>suggest what the problem really is.
I ran this code on Vista Home Premium JDK 1.6.0_14
public class StringLength {
public static void main(String... args) {
final int size = 1000;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < size; i++) {
sb.append('!');
}
String s = sb.toString();
System.out.println(s.length());
System.out.println(s);
}
}
It was 100% as expected.
Perhaps you should use a more complex pattern so you can see which
chars are disappearing.

Signature
Roedy Green Canadian Mind Products
http://mindprod.com
The USA and Iraq are as mismatched combatants as Mike Tyson and a bag lady. Americans make themselves look ridiculous when they justify invading and occupying Iraq in self defence.