Heap consumption and performance are real problems in Java.
But some great pratices in coding solve (or amenizes) the problem:
1) Don't repeat declaration of common used variables:
2) Create a thread in your main class taking a "forced garbage collection".
See the following program, that I've reorganized to avoid duplicated declarations, and
should work forever:
import java.io.*;
import java.net.*;
public class test extends Thread {
URL yahoo;
URLConnection yc;
Runtime rt = Runtime.getRuntime();
BufferedReader in;
String inputLine;
public static void main(String[] args) throws Exception {
new test( args );
}
public test( String [] args ) {
int arg;
// long mem; You dont use this code for nothing
arg = Integer.parseInt( args[ 0 ] );
this.start( );
for( int i = 0; i < arg; i ++ ) {
work( i );
}
}
private void work( int i ) {
try {
if( i % 100 == 0 ) {
System.out.println( i + ": " + rt.totalMemory( ) + "
bytes\n" );
}
// the declarations was transferred to class scope, avoiding
waste memory allocation
yahoo = new URL( "http://127.0.0.1/" );
yc = yahoo.openConnection( );
in = new BufferedReader( new InputStreamReader(
yc.getInputStream( ) ) );
while( ( inputLine = in.readLine( ) ) != null ) {
System.out.println(inputLine);
}
in.close( );
} catch( IOException e ){
}
}
public void run( ) {
while( true ) {
try {
this.sleep( 5000 ); // Make a forced Garbage Colection
every 5 seconds
rt.gc( );
} catch( Exception e ) {
}
}
}
}
Edson Richter
----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]