Re: Do you use a garbage collector (java vs c++ difference in "new")
asterisc wrote:
Why doesn't Java optimize this and allocate nothing, since they are
not used?
An interesting idea. Responding to your earlier request for an output
line for each deletion, I added a finalize method. Then I cut down the
number of loops to 1000, and ran it. I got this:
init:
deps-jar:
Compiling 1 source file to
C:\Users\Brenden\Dev\misc\FinalizeTest\build\classes
compile:
run:
finalizetest.Main@1eed786
Time: 2 ms
BUILD SUCCESSFUL (total time: 0 seconds)
Hmmm..... although increasing the loops does produce more output,
increasing the loops to one million only produces ~7k lines of total output.
Code below:
package finalizetest;
public class Main
{
private static final int LOOPS = 1000;
private static final int MODULUS = 5000000;
Main( int c )
{
count = c;
}
int count;
public static void main( String[] arg )
{
long start = System.currentTimeMillis();
for ( int i = 0; i < LOOPS; i++ )
{
Main test = new Main( i );
if ( i % MODULUS == 0 )
{
System.out.println( test );
}
}
long end = System.currentTimeMillis();
System.out.println( "Time: " + ( end - start ) + " ms" );
}
@Override
protected void finalize() throws Throwable
{
System.out.println( "Finalized: " + this );
super.finalize();
}
}