max internal length of String

A

Aryeh M. Friedman

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)
 
R

Roedy Green

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.
--
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 Party’s Over: Oil, War, and the Fate of Industrial Societies
 
S

Simon

Aryeh 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...

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
 
T

Tom Anderson

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
 
M

Mike Amling

Roedy 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.

32k elements? Arrays in Java are limited to 2G elements, 2**31-1 to
be precise.

--Mike Amling
 
R

Roedy Green

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.
 
R

Roedy Green

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.
 
R

Roedy Green

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.
 
M

Mike Amling

Roedy said:
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
 
M

Mike Schilling

Roedy said:
Oops. Showing my age. That's for DEC PDP 11 arrays. It seems
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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top