Edson (& java-linx),

this code below runs out of heap space even faster than
my original program.  sorry the code probably does not
look good after being pasted into this email..
but the basic -->test<-- is to write some code which
will 1) actually do something and 2) will run -->FOREVER<--
(well... 6 months would be acceptable)

i want to know that i can use the blackdown implementation
for serving a medium volume ecommerce site which is responsible
for millions of dollars of revenue.
so far, not so good.  but i am not a senior java developer,
so some inputs would be very much appreciated!

i can bascially make any code run out of heap (latest build of 1.2)
even if i use this in a loop and then fall out of scope:
Integer[] int_a = new Integer[100000];

in the code, you took a reference declaration and moved it
into class scope. i don't see how this does anything.
we are still calling new() in each iteration, and the garbage
collector SHOULD eventually free up the space.
the 5 second thread does not do anything more than making gc run a bit more 
often; memory just goes up and after 40000 iterations blackdown jdk
dies hard.
moving declarations into class scope is not a rule we should use
unless that variable is shared amongst many methods and/or
must persist across method calls.
why does it matter if i repeat the use of new()?????????
i am not allocating the whole heap before the gc runs!!!!!!!!!


Edson wrote:
 >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 newtest extends Thread {
        URL yahoo;
        URLConnection yc;
        Runtime rt = Runtime.getRuntime();
        BufferedReader in;
        String inputLine;
     int thflag;
        public static void main(String[] args) throws Exception {
                new newtest( args );
        }

        public newtest( String [] args ) {
                int arg;
                 thflag = 0;

                arg = Integer.parseInt( args[ 0 ] );
                this.start( );

                for( int i = 0; i < arg; i ++ ) {
                        work( i );
                }
                 thflag = 1;
        }

     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 
wasted 
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); don't care about the 
output..
                        }

                        in.close( );
                } catch( IOException e ){
                }
        }
        
        public void run( ) {
                while(thflag == 0 ) {
                        try {
                                this.sleep( 5000 ); // Make a forced Garbage Colection 
every 5 seconds
                                rt.gc( );
                        } catch( Exception e ) {
                        }
                }
        }
}



----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to