Garbage Collector Types

Overview

Every object that is created in java is stored in heap space.
The allocation and de-allocation of memory spaces for the objects is executed by automatic garbage collection process triggered by JVM.
Based on the flavor and version of JVM, different garbage collection algorithms are available in JVM, which can be specified via jvm parameters.
Java uses generational heap space where the referenced objects are transferred across different generations and garbage collection triggered based on heap space generational role which is termed as minor GC and major GC.

Types of Garbage Collectors

  • The Serial Garbage Collector.
  • The Parallel Garbage Collector.
  • The Concurrent Mark Sweep Garbage Collector.
  • The G1 Garbage Collector.

The Serial Garbage Collector.

As the name specifies here minor and major gcs are done serially using single CPU.
During the gc process all other application threads are paused.
This uses mark-compact-collection method, there by which moves existing object references to beginning of heap so that new memory allocations are done in single continuous chunk at the end of heap.

    Relevance:

This is suitable where we have minimal memory and few cores.
This can also be suitable where more JVMs than the available cores are executed on same machine.
This being stop-of-world process and single-threaded may not be suitable for server environments which demands high-throughput/better responsiveness.

    Jvm arguments::

    -XX:+UseSerialGC - turns on serial garbage collector.

The Parallel Garbage Collector.

This is also called throughput collector.
This uses multiple threads for accelerating garbage collection process.
During the gc process all other application threads are paused.
Based on java version used there are two jvm arguments for parallel gc.
This process reduces the overall time spent by the application for garbage collection.

    Relevance:

This is mostly useful for jvm process executing on multi core machines.
This collector is more suitable for batch jobs and more database intensive query operations.<

    Jvm arguments::

-XX:+UseParallelGC or -XX:+UseParallelOldGC  turns on parallel garbage collector.

Note: By default based on jvm version enabling one of the flag automatically enables the other flag unless it is disabled

The Concurrent Mark Sweep Garbage Collector.

This is also referred as concurrent low pause collector.
CMS uses the same parallel GC algorithm for younger generation objects.
This uses multiple threads to trace the reachable objects to identify objects for marking and removal.
The CMS garbage collector pauses the application threads during initial-marking (mark live objects reachable by gc roots) and re-mark (objects that become un-referenced after initial marking) phases.
All the remaining garbage collection process like tracing and sweeping of live objects run concurrently with application threads.
The CMS garbage collector uses more CPU resources than parallel garbage collector but with relatively lower pause times.

    Relevance:

This is suitable for applications with large tenured generation objects and has more CPU resources.
This is required for applications which need higher responsiveness like web applications/services.

    Jvm arguments::

  -XX:+ UseConcMarkSweepGC - turns on CMS  garbage collector

The G1 Garbage Collector.

The G1 collector is parallel, concurrent and incrementally compacting low pause garbage collector.
This also operates concurrently together with application threads.
G1 collector does compaction of heap space on every cycle where as CMS does compaction only on stop the world process.
Other garbage collector uses fixed sizes for each generational heap space where as G1 uses different heap approach where heap is portioned into equal sized regions which are allocated to different generational roles based on demand, this approach provides better flexibility in heap allocation.
This heap distribution approach of G1 helps in having relatively lesser heap sizes.
G1 prioritizes its collection from the areas which has maximum reclaimable heap regions there by yielding maxing free space after gc.
G1 also has more predictable pause times as it can select the regions for gc which may fit into the configured pause interval.

    Relevance:

This is mostly suitable for applications with has large heap and need predictable low gc pause times.

    Jvm arguments::

 -XX:+UseG1GC - turns on G1 garbage collector.

Conclusion.

Each garbage collector can be fine tuned by configuring various jvm arguments which are specific to each gc collector type.
Java Ergonomics introduced in jdk 1.5, decides on default values for jvm parameters based on available system resources.
We can configure and fine tune these default values by overriding various jvm arguments, which will be mainly required when running multiple jvms on the same machine.

Also Read:

Advertisements

Basic NodeEclipse Setup

Overview

Eclipse is an IDE which is built in java and is mostly used for java development. Eclipse also supports multiple other languages through use of plugins.
Eclipse has some of the javascript plugins which aid in javascript development.
In recent years node.js has become one of the major platform for building javascript applications. NodeEclipse is an Eclipse plugin for node.js.

NodeEclipse Installation:

  • Install node js on Local machine.
  • Click Eclipse Help -> Eclipse Market place.
  • Type Node under Find and Click GO.
  • In the displayed list select NodeEclipse and click Update/Install.
  • Select the relevant packages and click Next and finally install.

Configuring NodeEclipse:

On successful installation of Node Eclipse, we need to configure it via Eclipse -> Window -> Preferences

  • Under preferences we see NodeEclipse.
  • On click of NodeEclipse, we have all the configuration details for Node.js running used from eclipse.
  • Revalidate Node.js path and click OK.

Conclusion:

After above setup, Node.js based programming can be used from eclipse itself.

Inconsistent Arithmetic Operations On Decimal Values

Overview

This post provides details on general pitfalls while doing arithmetic operations on decimal values and how to handle them..

Subtraction operation:

  • Executed statements:
    		System.out.println(&quot;Actual value of (4.375-4.250) Using Double:  &quot;+((4.375-4.250))+&quot; Expected Result 0.125&quot;);
    		System.out.println(&quot;Actual value of (4.650-4.250) Using Double:  &quot;+((4.650-4.250))+&quot; Expected Result 0.4&quot;);
    		System.out.println(&quot;Actual value of (4.650-4.250) Using BigDecimal:  &quot;+(BigDecimal.valueOf(4.650).subtract(BigDecimal.valueOf(4.250)))+&quot; Expected Result 0.4&quot;);
    		
  • Results:
    In the below results, the double subtraction is correct for (4.375-4.250) but incorrect for (4.650-4.250).

    		Actual value of (4.375-4.250) Using Double:  0.125 Expected Result 0.125
    		Actual value of (4.650-4.250) Using Double:  0.40000000000000036 Expected Result 0.4
    		Actual value of (4.650-4.250) Using BigDecimal:  0.40 Expected Result 0.4
    		

Addition operation:

  • Executed statements:
    		System.out.println(&quot; Actual value of (4.65+4.25) Using Double:  &quot;+(4.65+4.25)+&quot; Expected Result 8.90&quot;);
    		System.out.println(&quot; Actual value of (0.111+3.70) Using Double:  &quot;+(0.111+3.70)+&quot; Expected Result 3.811&quot;);
    		System.out.println(&quot; Actual value of (0.111+3.70) Using BigDecimal:  &quot;+(BigDecimal.valueOf(0.111).add(BigDecimal.valueOf(3.70)))+&quot; Expected Result 3.811&quot;);
    		
  • Results:
    In the below results, the double addition is correct for (4.65+4.25) but incorrect for (0.111+3.70).

    		Actual value of (4.65+4.25) Using Double:  8.9 Expected Result 8.90
    		Actual value of (0.111+3.70) Using Double:  3.8110000000000004 Expected Result 3.811
    		Actual value of (0.111+3.70) Using BigDecimal:  3.811 Expected Result 3.811
    		

Multiplication operation:

  • Executed statements:
    		System.out.println(&quot; Actual value of (100*0.21) Using Double:  &quot;+(100*0.21)+&quot; Expected Result 21.0&quot;);
    		System.out.println(&quot; Actual value of (100*0.211) Using Double:  &quot;+(100*0.211)+&quot; Expected Result 21.1&quot;);
    		System.out.println(&quot; Actual value of (100*0.211) Using BigDecimal:  &quot;+(BigDecimal.valueOf(100).multiply(BigDecimal.valueOf(0.211)))+&quot; Expected Result 21.100&quot;);
    		System.out.println(&quot; Using scale for Bigdecimal results:  &quot;+(BigDecimal.valueOf(100).multiply(BigDecimal.valueOf(0.211))).setScale(1)+&quot; Expected Result 21.1&quot;);
    		
  • Results:
    In the below results, the double multiplication is correct for (100*0.21) and incorrect for (100*0.211).

    			Actual value of (100*0.21) Using Double:  21.0 Expected Result 21.0
    			Actual value of (100*0.211) Using Double:  21.099999999999998 Expected Result 21.1
    			Actual value of (100*0.211) Using BigDecimal:  21.100 Expected Result 21.100
    			Using scale for Bigdecimal results:  21.1 Expected Result 21.1
    		

Analysis:

Decimal values are based on power of 2, for arithmetic operations java internally approximates to nearest power of 2 which results in above absurd results. When a exact calculation is feasible with representation of nearest power of 2 we get expected results in all other scenario we get some approximate results. The BigDecimal offers various methods for performing arithmetic operations and to the required precision.