>. My question is do I have to close all these streams
>regardless of using in my program or not to prevent any resource leak?
my understanding is YES, even if you don't use them.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
On Dec 17, 9:56 pm, sunkeun...@gmail.com wrote:
> Hi,
>
[quoted text clipped - 28 lines]
> Thanks,
> Sun
Your question is interesting. Below is a simple testing program I
devised to experimentally answer the question.
This program will create a user specified number of processes. The
user can specify whether the program will maintain a hard or soft
reference to the process and whether it will close the streams
associated with the process.
When I asked the program to create 339 processes without closing the
associated streams, the program created 338 processes and crashed on
the 339th. However when I asked the program to create 339 processes
and close the associated streams, the program ran successfully. This
experiment suggests that there is a resource leakage associated with
not closing the process associated streams.
I hope this helps.
Emory Merryman
External Concepts Guild
final class TestLeaking
{
public static final void main ( final java . lang . String [ ]
args ) throws java . lang . Exception
{
final int count = java . lang . Integer . parseInt ( args [ 0 ] ) ;
int i = 0 ;
final boolean strongRef = java . lang . Boolean . parseBoolean ( args
[ 1 ] ) ;
final boolean weakRef = java . lang . Boolean . parseBoolean ( args
[ 2 ] ) ;
final boolean closeInputStream = java . lang . Boolean . parseBoolean
( args [ 3 ] ) ;
final boolean closeOutputStream = java . lang . Boolean .
parseBoolean ( args [ 4 ] ) ;
final boolean closeErrorStream = java . lang . Boolean . parseBoolean
( args [ 5 ] ) ;
final long start = new java . util . Date ( ) . getTime ( ) ;
final java . lang . Runtime runtime = java . lang . Runtime .
getRuntime ( ) ;
try
{
for ( i = 0 ; i < count ; i ++ )
{
process ( runtime , strongRef , weakRef , closeInputStream ,
closeOutputStream , closeErrorStream ) ;
}
}
finally
{
final long stop = new java . util . Date ( ) . getTime ( ) ;
final int k = processes . size ( ) ;
final long elapsed = stop - start ;
java . lang . System . out . println ( i + "\t" + count + "\t" +
strongRef + "\t" + weakRef + "\t" + closeInputStream + "\t" +
closeOutputStream + "\t" + closeErrorStream + "\t" + start + "\t" +
stop + "\t" + elapsed + "\t" + k ) ;
}
}
private static java . util . List < java . lang . Process >
processes = new java . util . ArrayList < java . lang . Process >
( ) ;
public static final void process ( final java . lang . Runtime
runtime , final boolean strongRef , final boolean weakRef , final
boolean closeInputStream , final boolean closeOutputStream , final
boolean closeErrorStream ) throws java . io . IOException
{
final java . lang . Process process = runtime . exec ( "ls" ) ;
if ( strongRef )
{
processes . add ( process ) ;
}
if ( weakRef )
{
waitForGC ( process ) ;
}
if ( closeInputStream )
{
final java . io . InputStream stream = process . getInputStream
( ) ;
stream . close ( ) ;
}
if ( closeOutputStream )
{
final java . io . OutputStream stream = process . getOutputStream
( ) ;
stream . close ( ) ;
}
if ( closeErrorStream )
{
final java . io . InputStream stream = process . getErrorStream
( ) ;
stream . close ( ) ;
}
}
public static final void waitForGC ( final java . lang . Process
process )
{
final java . lang . ref . Reference < java . lang . Process > ref =
new java . lang . ref . WeakReference < java . lang . Process >
( process ) ;
final java . lang . Runnable runnable = new java . lang . Runnable
( )
{
public final void run ( )
{
while ( ref . get ( ) != null )
{
}
}
} ;
final java . lang . Thread thread = new java . lang . Thread
( runnable ) ;
thread . start ( ) ;
}
}
sunkeunlee@gmail.com - 18 Dec 2007 06:55 GMT
On Dec 17, 6:40 pm, External Concepts Guild <REE9o...@gmail.com>
wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
[quoted text clipped - 162 lines]
>
> - Show quoted text -
Thanks all. I agree with your conclusion. The reason I posted this
question is I have an outofmemory error: unable to create a native
thread from a method that creates a Process and I found the code
doesn't close streams properly. If I don't call this metod the error
disappears. By the way, some reason our code creates more than 600
threads. Now I know how to resolve this issue.
Thanks,
Sun