Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Facing memory problem (OutofMemoryException) in StringBuffer class 1

Status
Not open for further replies.

vavkkishore

Programmer
Mar 8, 2001
1
0
0
US
Hi Java gurus,

I have a question and facing that problem now.

I working on an application where I have to read say 1000 (approximate value, may be less or more than this value in future), files where each file contains areound 3000 lines. User will give me folder name from where I have to read the files in that folder and their contents. We want it in a a multithreaded approach. I know all the file names in that folder. After knowing the file names, I wrote a thread class which will take the file name and read the contents of the file. I set maximum threads as say 10. So 10 threads are reading 10 files each containing around 3000 lines. I have a threadmanager for this, which will create the above threads. I also had a StringBuffer object in ThreadManager class which is a global one to all the threads. Once a thread completes its reading of file, it will call setFileContents method in the threadmanager, which will add that string to the global StrinbBuffer object and this method is synchronized one.

Problem is, when there are say 100 files, each file having 3000 lines, then this StringBuffer is coming out of the application because of OutofMemoryException.

Once I get the contents of all the files, ThreadManager will write it to an AWT component showing the contents of all the files.

My question is, how to come out of that problem, I change the heap size also at java command options, secondly what are the best approaches for this multithread application, so that the contents of all the files will come in an order of the file names and those contents are shown in the AWT component.

I want it urgently and I will be thankful for that.

Thanks in advance for your advices.

 
are you sure to need StringBuffer?
If you just need to read the content, you can just use String. Stringbuffer is only useful for strings that you need to edit. Moreover they need more memory and are very slow.
 
oh, okay, i see why you need StringBuffer.. sorry i skipped some lines. :/what kind of awt component do you wish to use for displaying?
 
This is directly from Sun.
class StringBuffer
the default string size is 16 characters, set to a much larger size if you can make any kind of intelligent guess on the maximum size up to the JVM maximum of 32k or 64k characters depending on your JVM/JRE.

So you problem lies there. your text is too long. You will have to use an other method.
Maybe your text button type can allow to work on other kind of input for the text enclosed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top