 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Jean Pierre Daviau Guest
|
Posted: Wed Dec 13, 2006 9:15 pm Post subject: StringBuffer java.lang.OutOfMemoryError |
|
|
Hi to everyones,
java.lang.OutOfMemoryError
at java.lang.StringBuffer.insert(Compiled Code)
There is about 8200 characters in the buffer including about 257
commas.
strbuf.setLength(strbuf.length()+1000);
for (int j=0;j<strbuf.length() ;j++ )
{
if(strbuf.charAt(j) == ',')
strbuf.insert(j, "\r\n");
}
--
Thanks for your attention.
Jean Pierre Daviau
--
windows Xp
asus p4 s533/333/133
Intel(R) Celeron (R) CPU 2.00 GHz
Processor Radeon7000 0x5159 agp |
|
| Back to top |
|
 |
Eric Sosman Guest
|
Posted: Wed Dec 13, 2006 10:20 pm Post subject: Re: StringBuffer java.lang.OutOfMemoryError |
|
|
Jean Pierre Daviau wrote:
| Quote: | Hi to everyones,
java.lang.OutOfMemoryError
at java.lang.StringBuffer.insert(Compiled Code)
There is about 8200 characters in the buffer including about 257
commas.
strbuf.setLength(strbuf.length()+1000);
for (int j=0;j<strbuf.length() ;j++ )
{
if(strbuf.charAt(j) == ',')
strbuf.insert(j, "\r\n");
}
|
You don't understand what the insert() method does. The
loop you have written will attempt to insert an infinite number
of "\r\n" pairs before the first comma in the buffer; you are
safe only if the buffer contains no commas at all.
--
Eric Sosman
esosman@acm-dot-org.invalid |
|
| Back to top |
|
 |
Jean Pierre Daviau Guest
|
Posted: Wed Dec 13, 2006 10:29 pm Post subject: Re: StringBuffer java.lang.OutOfMemoryError |
|
|
| Quote: | You don't understand what the insert() method does. The
loop you have written will attempt to insert an infinite number
of "\r\n" pairs before the first comma in the buffer; you are
safe only if the buffer contains no commas at all.
Is there a way to do that? |
Whit append()? |
|
| Back to top |
|
 |
Jean Pierre Daviau Guest
|
Posted: Wed Dec 13, 2006 10:34 pm Post subject: Re: StringBuffer java.lang.OutOfMemoryError |
|
|
I try to be clear. I want a java mailer to send a email with broken strings.
String astring = "This, is a, string.";
will become
astring = "This
is a
string."
PrintStream msg = smtp.startMessage();
msg.print(astring ); |
|
| Back to top |
|
 |
Eric Sosman Guest
|
Posted: Wed Dec 13, 2006 10:50 pm Post subject: Re: StringBuffer java.lang.OutOfMemoryError |
|
|
Jean Pierre Daviau wrote:
| Quote: | I try to be clear. I want a java mailer to send a email with broken strings.
String astring = "This, is a, string.";
will become
astring = "This
is a
string."
|
astring = astring.replaceAll(",", "\r\n");
Notes:
The first argument is a regular expression, not a "plain"
string. "," will make no trouble, but some other characters
have special meanings and would need special treatment. This
could be an advantage, though: for example, to turn commas into
line breaks and delete the surrounding white space you might
write replaceAll("\\s*,\\s*", "\r\n").
The code shown is fine for a few strings, but it compiles
the regular expression every time you execute it. If you want
to make the replacement on many strings, you may be better off
to compile the expression just once with the Pattern class and
then re-use the already-compiled regex for each string. See
the Javadoc for String's replaceAll, and follow the links for
Pattern and Matcher.
--
Eric Sosman
esosman@acm-dot-org.invalid |
|
| Back to top |
|
 |
Hendrik Maryns Guest
|
Posted: Wed Dec 13, 2006 10:56 pm Post subject: Re: StringBuffer java.lang.OutOfMemoryError |
|
|
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Jean Pierre Daviau schreef:
| Quote: | You don't understand what the insert() method does. The
loop you have written will attempt to insert an infinite number
of "\r\n" pairs before the first comma in the buffer; you are
safe only if the buffer contains no commas at all.
Is there a way to do that?
Whit append()?
|
Please quote some context when replying to a message.
You have to increment the loop counter with the number of characters you
added, since you insert the linebreak (have a look at
System.getProperty(line.separator)) /before/ the comma, so it finds it
again. But probably you just want replace(int,char).
strbuf.setLength(strbuf.length()+1000);
What is this good for?
for (int j=0;j<strbuf.length() ;j++ )
{
if(strbuf.charAt(j) == ','){
strbuf.insert(j++, System.getProperty(line.separator));
}
}
or
for (int j=0;j<strbuf.length() ;j++ )
{
if(strbuf.charAt(j) == ','){
strbuf.replace(j, System.getProperty(line.separator));
}
}
Untested, you might need to change some minor stuff.
And oh, use brackets for if statements, even if they only contain one
expression.
HTH, H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
iD8DBQFFgDC3e+7xMGD3itQRAux1AJ9od0x63YVgVIEADoF/x3btR+9+GQCfaaes
1R2Ldzd+3KcNIc+oiTc2GWc=
=6mBf
-----END PGP SIGNATURE----- |
|
| Back to top |
|
 |
Jean Pierre Daviau Guest
|
Posted: Thu Dec 14, 2006 12:25 am Post subject: Re: StringBuffer java.lang.OutOfMemoryError |
|
|
| Quote: | strbuf.setLength(strbuf.length()+1000);
What is this good for?
I thought I was adding more characters... |
|
|
| Back to top |
|
 |
Oliver Wong Guest
|
Posted: Thu Dec 14, 2006 2:21 am Post subject: Re: StringBuffer java.lang.OutOfMemoryError |
|
|
"Hendrik Maryns" <hendrik_maryns (AT) despammed (DOT) com> wrote in message
news:elpbbn$let$1 (AT) newsserv (DOT) zdv.uni-tuebingen.de...
| Quote: |
for (int j=0;j<strbuf.length() ;j++ )
{
if(strbuf.charAt(j) == ','){
strbuf.insert(j++, System.getProperty(line.separator));
}
}
|
This code seems to assume that the size of a line separator is always 1
char, but this is not true for Windows, for example. Maybe something like
this: (also untested):
final String separator = System.getProperty(line.separator);
final int separatorLength = separator.length();
for (int j=0;j<strbuf.length() ;j++ )
{
if(strbuf.charAt(j) == ','){
strbuf.insert(j, separator);
j += separatorLength;
}
}
(But Eric's suggestion to use a regexp is probably better.)
- Oliver |
|
| Back to top |
|
 |
Thomas Hawtin Guest
|
Posted: Thu Dec 14, 2006 5:20 am Post subject: Re: StringBuffer java.lang.OutOfMemoryError |
|
|
Oliver Wong wrote:
| Quote: |
for (int j=0;j<strbuf.length() ;j++ )
{
if(strbuf.charAt(j) == ','){
strbuf.insert(j, separator);
j += separatorLength;
}
}
|
Erk, it's an O(n^2) algorithm. You'd never get passed a Google interview. ;)
| Quote: | (But Eric's suggestion to use a regexp is probably better.)
|
As ever, anything that separates what you are writing from what you are
reading is generally a good idea.
Tom Hawtin |
|
| Back to top |
|
 |
Thomas Hawtin Guest
|
Posted: Thu Dec 14, 2006 9:57 pm Post subject: Re: StringBuffer java.lang.OutOfMemoryError |
|
|
Oliver Wong wrote:
| Quote: | "Thomas Hawtin" <usenet (AT) tackline (DOT) plus.com> wrote in message
news:45808a4a$0$8757$ed2619ec@ptn-nntp-reader02.plus.net...
Oliver Wong wrote:
for (int j=0;j<strbuf.length() ;j++ )
{
if(strbuf.charAt(j) == ','){
strbuf.insert(j, separator);
j += separatorLength;
}
}
Erk, it's an O(n^2) algorithm. You'd never get passed a Google interview.
;)
Where n is the square root of the length of the input? It seems to be
that it will always do as many "reads" (strbuf.charAt()) as there are
characters in the input, and in the worst case, it will do as many writes
(strbuf.insert()) as there are characters (if the input consists entirely of
commas).
|
StringBuffer.insert is an O(n) operation. Assuming there are O(n) commas
in each of the strings, then we have O(n) * O(n) => O(n^2) algorithm.
Tom Hawtin |
|
| Back to top |
|
 |
Jean Pierre Daviau Guest
|
Posted: Thu Dec 14, 2006 10:28 pm Post subject: Re: StringBuffer java.lang.OutOfMemoryError |
|
|
The newbe is asking his self
| Quote: | StringBuffer.insert is an O(n) operation.
??? |
|
|
| Back to top |
|
 |
Thomas Hawtin Guest
|
Posted: Thu Dec 14, 2006 10:37 pm Post subject: Re: StringBuffer java.lang.OutOfMemoryError |
|
|
Jean Pierre Daviau wrote:
| Quote: | The newbe is asking his self
StringBuffer.insert is an O(n) operation.
???
|
!!!
It has to copy the n following characters the appropriate distance up
the array (backwards). It will therefore take a length of time roughly
proportional to the length of the buffer.
If you do that operation a number of times roughly proportional to the
buffer length, then you will end up with an O(n^2) algorithm.
Tom Hawtin |
|
| Back to top |
|
 |
Jean Pierre Daviau Guest
|
Posted: Thu Dec 14, 2006 10:53 pm Post subject: Re: StringBuffer java.lang.OutOfMemoryError |
|
|
"Thomas Hawtin" <usenet (AT) tackline (DOT) plus.com> a écrit dans le message de news:
45817d5b$0$8753$ed2619ec@ptn-nntp-reader02.plus.net...
| Quote: | Jean Pierre Daviau wrote:
The newbe is asking his self
StringBuffer.insert is an O(n) operation.
???
!!!
It has to copy the n following characters the appropriate distance up the
array (backwards).
|
It re reads everytime from the end? It would be the same if it would be
reading from the beginning?
do you have another example? |
|
| Back to top |
|
 |
Thomas Hawtin Guest
|
Posted: Thu Dec 14, 2006 11:04 pm Post subject: Re: StringBuffer java.lang.OutOfMemoryError |
|
|
Jean Pierre Daviau wrote:
| Quote: | "Thomas Hawtin" <usenet (AT) tackline (DOT) plus.com> a écrit dans le message de news:
45817d5b$0$8753$ed2619ec@ptn-nntp-reader02.plus.net...
Jean Pierre Daviau wrote:
The newbe is asking his self
StringBuffer.insert is an O(n) operation.
???
!!!
It has to copy the n following characters the appropriate distance up the
array (backwards).
It re reads everytime from the end? It would be the same if it would be
reading from the beginning?
do you have another example?
|
Are you asking why I said backward? It would probably be slightly faster
to do it forwards, but done naively it wouldn't work.
Say I had a string buffer containing 'a', 'b', 'c'. Now I insert 'x'
between 'a' and 'b'. Copying forward chars[2] = chars[1] gives 'a', 'b',
'b'; chars[3] = chars[2] gives 'a', 'b', 'b', 'b'. So we end up with the
string "axbb" instead of "axbc".
Tom Hawtin |
|
| Back to top |
|
 |
Tor Iver Wilhelmsen Guest
|
Posted: Thu Dec 14, 2006 11:38 pm Post subject: Re: StringBuffer java.lang.OutOfMemoryError |
|
|
Thomas Hawtin <usenet (AT) tackline (DOT) plus.com> writes:
| Quote: | It has to copy the n following characters the appropriate distance up
the array (backwards). It will therefore take a length of time roughly
proportional to the length of the buffer.
|
But that uses System.arrayCopy(), which can be assumed to be highly
optimized compared to the "manual" code equivalent would be. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|