Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
The second piece of code for the Software Sympathy blog post. This uses the same Ballast class as the first example
package gc;
import gc.ballast.Ballast;
public class RegionSizeGamesTake3
{
public static final int RegionsToAllocate = 128;
public static void main( String[] args )
{
RegionSizeGamesTake3 carrier = new RegionSizeGamesTake3();
// Hotspot, deal with these please
for ( int i = 0; i < 1000; i++ )
{
if ( i % 100 == 0)
{
System.out.println("Warmup round " + i + " reached");
}
carrier.firstRun();
carrier.secondRun();
}
long startTime = System.currentTimeMillis();
for( int i = 0; i < 5000; i++ )
{
carrier.firstRun();
}
System.out.println( "First run took " + (System.currentTimeMillis() - startTime) + "ms" );
startTime = System.currentTimeMillis();
for( int i = 0; i < 5000; i++ )
{
carrier.secondRun();
}
System.out.println("Second run took " + (System.currentTimeMillis() - startTime) + "ms");
}
private void firstRun()
{
Region[] regions = new Region[ RegionsToAllocate ];
for ( int i = 0; i < regions.length; i++ )
{
regions[i] = new Region();
regions[i].setValue();
}
}
private void secondRun()
{
Region[] regions = new Region[ RegionsToAllocate ];
for ( int i = 0; i < regions.length; i++ )
{
regions[i] = new Region();
regions[i].setReference();
}
}
private static class Region
{
private static final Object Reference = new Object();
private final Ballast[] stuff;
public Region()
{
this.stuff = new Ballast[ 16384 ];
for ( int i = 0; i < stuff.length; i++ )
{
stuff[i] = new Ballast();
}
}
public Ballast at( int i )
{
return stuff[i];
}
public void setValue()
{
for ( int i = 0; i < stuff.length; i++ )
{
stuff[i].setLongValue( 42l );
}
}
public void setReference()
{
for ( int i = 0; i < stuff.length; i++ )
{
stuff[i].setReference( stuff[i] );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment