Skip to content
Snippets Groups Projects
Commit 094d9b8f authored by Goik Martin's avatar Goik Martin
Browse files

Prime decomposition example.

Enlarging the Java VM's heap space.
parent abe2e1f2
No related branches found
No related tags found
No related merge requests found
Doc/Sd1/Ref/Screen/primDriverRunConfigOverview.bmp

1.5 MiB

Doc/Sd1/Ref/Screen/primDriverRunConfigVmOptions.bmp

1.5 MiB

......@@ -481,6 +481,103 @@ public class FactorTest {
<para>Check for second prime factor 3 of frequency 1.</para>
</callout>
</calloutlist>
<para>For better illustration we provide the following example:</para>
<programlisting language="java">public class Driver {
/**
* @param args Unused
*/
public static void main( String[] args ) {
final Sieve sieve = new Sieve(Integer.MAX_VALUE);
int value = Integer.MAX_VALUE;
System.out.println("Value " + value + " is " +
(sieve.isPrime(value)? "" : "not") + " prime.");
value--; // Even Value
System.out.println("Value " + value + " is " +
(sieve.isPrime(value)? "" : "not") + " prime.");
value --; // Non-prime as well.
System.out.println("Value " + value + " is " +
(sieve.isPrime(value)? "" : "not") + " prime.");
value = Integer.MAX_VALUE - 1;
final PrimeFrequencySet pfs = sieve.getPrimeFactors(value);
System.out.print(value + " = ");
String separator = "";
for (PrimeFrequency pf : pfs.get()) {
for (int i = 0; i &lt; pf.getFrequency(); i++) {
System.out.print(separator + pf.prime);
separator = " * ";
}
}
System.out.println();
}
}</programlisting>
<para>This yields the following output:</para>
<programlisting language="none">Value 2147483647 is prime.
Value 2147483646 is not prime.
Value 2147483645 is not prime.
2147483646 = 2 * 3 * 3 * 7 * 11 * 31 * 151 * 331</programlisting>
<para>Depending on your implementation you may encounter the following
heap memory related error:</para>
<programlisting language="none">Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at de.hdm_stuttgart.mi.prim.sieve.Sieve.initSieve(Sieve.java:75)
at de.hdm_stuttgart.mi.prim.sieve.Sieve.&lt;init&gt;(Sieve.java:60)
at de.hdm_stuttgart.mi.prim.Driver.main(Driver.java:19)</programlisting>
<para>The <xref linkend="glo_Java"/> virtual machine by default may not
provide enough heap space. You may enlarge the default allocation by
setting the <option>-Xmx</option>. Eclipse provides a convenient way by
means of a run time configuration (Menu Run --&gt; Run
Configurations):</para>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="Ref/Screen/primDriverRunConfigOverview.bmp"/>
</imageobject>
</mediaobject>
</informalfigure>
<para>Clicking the <quote>Arguments</quote> tab allows for setting the
respective VM heap setting:</para>
<informalfigure>
<mediaobject>
<imageobject>
<imagedata fileref="Ref/Screen/primDriverRunConfigVmOptions.bmp"/>
</imageobject>
</mediaobject>
</informalfigure>
<para>This allows for running your application with a maximum of 4
gigabytes heap space.</para>
<para>With respect to automated unit testing (<command>mvn</command>
<option>test</option>)you may want to set the same value in you
<filename>pom.xml</filename> file</para>
<programlisting language="xml">&lt;plugin&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
&lt;version&gt;2.19&lt;/version&gt;
&lt;configuration&gt;
<emphasis role="bold">&lt;argLine&gt;-Xmx4G&lt;/argLine&gt;</emphasis>
&lt;/configuration&gt;
&lt;/plugin&gt;</programlisting>
</section>
</section>
</appendix>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment