Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Max size of data a string variable can hold?

 
john klucas
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any one tell me, what i have to do if the String Object holds data more than 20-22MB of data.
The string object holds that much of data and then the same data have to be displayed in a text area.
i am doing something like this:

private void doReadFile(BufferedReader fileReader) {
String strial = "";
String s = "";
try{
if(fileReader != null)
{
ivjTextArea1.append("One Moment Please.....");
while ((s=fileReader.readLine()) != null)
{
temptrial.append(s+"\n");
}
strial = temptrial.toString();
ivjTextArea1.setText(temptrial.toString());
fileReader.close();
}
} catch(Throwable exception){
handleException(exception);
}
}
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't post the exact same question in multiple forums. You just waste peoples time.
 
john klucas
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i will really appreciate if any one answers to my question.
i did post it twice, bcoz there was no reply.
Anyway soory for posting it twice.]
Can any one answer please.
Urgent please.
 
James Gray
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I totally understand the question, but as far as I know the size of a String is limited only by the resources of the machine.
Looking at your code, my biggest suggestion would be to use TextArea's append() method directly. Hope that helps.
 
Geoffrey Falk
Ranch Hand
Posts: 171
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is probably a bad idea.
Depending on the JVM implementation, it is likely that creating a 22MB string will attempt to create a contiguous chunk of memory on the heap. This will probably result in swapping, and the performance of your application will be crap.
If you are doing a lot of string manipulations, it will be even worse. Every time you do change the string, it will try to allocate another 22MB to hold the changed data. You could have the VM holding on to hundreds of MB at once.
Think about what you are trying to achieve. If you are writing a text editor, for instance, maybe holding each line in a separate string is the best approach.
Java has great features for memory management (like automatic GC), but you still need to understand basic computer science principles if you want to write efficient code.
 
Dragon Liu
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Geoffrey Falk:
I agree with you very much!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic