wood burning stoves 2.0*
The moose likes Java in General and the fly likes Process waits forever when using Stringbuffer with more than 2000 strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Process waits forever when using Stringbuffer with more than 2000 strings" Watch "Process waits forever when using Stringbuffer with more than 2000 strings" New topic
Author

Process waits forever when using Stringbuffer with more than 2000 strings

Vivek Jain
Ranch Hand

Joined: Oct 17, 2007
Posts: 34
Hi,
I am using ProcessBuilder to invoke an external command which will provide me the output. And I'm supplying input through String Buffer. My program works fine for 2934 strings within StringBuffer. However, the moment my stringbuffer size goes beyond 2934, the program hangs & waits forever.

Below is my code.




Can anyone tell me what could be the issue? Is there any limit in String Buffer? If so, how to resolve it?

PS: I also tried specifying large capacity such as 99999 in constructor of String buffer but still no luck....

Thanks!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40079
    
  28
Are you really writing of .append("string"); 2000 times? That might take the method size over the maximum allowed.

And this is too difficult a question for beginners. Moving.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40079
    
  28
And what are you doing with the input and error streams from the Process? You know you have to "drain" them both, otherwise the Process may suffer deadlock.
Vivek Jain
Ranch Hand

Joined: Oct 17, 2007
Posts: 34
Yes....I'm in fact giving input of 9000 strings within String Buffer.

Using , I'm actually capturing the output of the external utility which I am calling using ProcessBuilder.

Can you please help?

It looks like the buffer cannot accommodate much data. I just tried replacing String Buffer with Array List but the problem stays the same. So, it could be related to the output stream being not able to handle much data.

ok....I just noticed that the program works fine for small chunk of strings say 500 strings. However, if I use more than this limit, it just hangs at line process.waitFor().

What could be the resolution?

Thanks!

Vivek Jain
Ranch Hand

Joined: Oct 17, 2007
Posts: 34
Hi,
After doing more analysis, I noticed that the process is hanging because the I/O buffer is full...And it should be released by reading it.

If I try the below code, it works fine...But again it also has the limitation of processing around 8000 strings.....and thats not the right approach since I'm reading it before even knowing whether the process has finished it or not....So is there any other better soln???



thanks!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19799
    
  20

Someone here hasn't read the Javaworld article "When Runtime.exec won't"


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Vivek Jain
Ranch Hand

Joined: Oct 17, 2007
Posts: 34
Rob,
I've read the article at Javaworld. Appreciate if you can tell me the solution for my case?

Thanks!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19799
    
  20

Try reading the error stream as well, in a different thread.
Vivek Jain
Ranch Hand

Joined: Oct 17, 2007
Posts: 34
Hi Rob,
Still no luck....Tried reading both error & input streams before waiFor....it hangs....When I do the Ctrl-C, it gives below error:



And if I look at the line number 2990 from above error, it seems like the issue is somewhere else.....Basically, when I'm throwing the string as an input to this external utility, it is hanging there itself....



Code hangs at Line 2990 which is writer.write(lst);

Can you please help fixing the issue?

Appreciate your help!!!
 
Don't get me started about those stupid light bulbs.
 
subject: Process waits forever when using Stringbuffer with more than 2000 strings