Saturday, May 4, 2019

Guide on Java String getBytes​

1. Overview:

Encodes the current String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.

This method has 4 overloaded methods.

public byte[] getBytes()
public void getBytes​(int srcBegin, int srcEnd, byte[] dst, int dstBegin) --> This is @Deprecated
public byte[] getBytes​(String charsetName) throws UnsupportedEncodingException
public byte[] getBytes​(Charset charset)



Java String getBytes

Friday, May 3, 2019

Maven Create A Java Project

Overview:

In this tutorial, We'll learn how to Setup Maven in eclipse and how to create a java.

Maven Create A Java Project

Eclipse Setup:


1) First Open eclipse click on Help -> Install New Software

Install-new-software


2) Opens a dialogue box then click on "add" button, provide "Maven" in name filed and "http://download.eclipse.org/technology/m2e/releases" in Location field. Click on add.

Thursday, May 2, 2019

Java 10 LVTI: Local Variable Type Inference Explained with Examples

Java 10 Local Variable Type Inference:

In this tutorial, We will learn what is Java 10 Local Variable Type Inference with examples.

Introduction:

Java 10 introduced a new shiny language feature called local variable type inference and One of the most obvious enhancements in JDK 10.
Type inference refers to the automatic detection of the datatype of a variable, done generally at the compiler time.

For the local variables, we can use a special reserved type "var" instead of actual type and var is not a keyword. This enhancement helps in reducing the boilerplate code.

Java 10 LVTI - Local Variable Type Inference Explained with Examples

Tuesday, April 30, 2019

Shenandoah: Ultra low-pause garbage collector in Java 12

Java 12 New ZGC - Concurrent Class Unloading

One feature ZGC is currently missing is class unloading before java 12 versions. This should be implemented. Due to the latency sensitive nature of class unloading, this operation should be performed concurrently.

Concurrent Class Unloading Released in JDK 12 for ZGC

The Z Garbage Collector now supports class unloading. By unloading unused classes, data structures related to these classes can be freed, lowering the overall footprint of the application. Class unloading in ZGC happens concurrently, without stopping the execution of Java application threads, and has zero impact on GC pause times. This feature is enabled by default, but can be disabled by using the command line option `-XX:-ClassUnloading`.


Shenandoah - A Low-Pause-Time Garbage Collector

Traditional Class Unloading VS ZGC Concurrnet Class Unloading


Traditional Class Unloading

Step 1: Marking (concurrent)
  Mark metadata (classes, CLDs) when marking objects


Step 2: Reference processing (STW)
  Need to know what is reachable from finalizers before class unloading


Step 3: Unloading (STW)
  Unload code cache
  Unload metadata

ZGC Concurrnet Class Unloading

Step 1: Marking (concurrent)
 Mark metadata (classes, CLDs) when marking objects
 Mark both strong and final reachable graphs


Step 2: Reference processing (concurrent)
 Already know what is reachable from finalizers before class unloading


Step 3: Unloading (concurrent)
 Unload code cache
 Unload metadata

Image of ZGC-Phases

Shenandoah: A Low-Pause-Time Garbage Collector:

Added a new garbage collection (GC) algorithm named Shenandoah which reduces GC pause times by doing evacuation work concurrently with the running Java threads. Pause times with Shenandoah are independent of heap size, meaning you will have the same consistent pause times whether your heap is 100 MB or 100 GB or 1TB.

Modern machines have more memory and more processors than ever before. Service Level Agreement (SLA) applications guarantee response times of 10-500ms. In order to meet the lower end of that goal we need garbage collection algorithms which are efficient enough to allow programs to run in the available memory, but also optimized to never interrupt the running program for more than a handful of milliseconds. Shenandoah is an open-source low-pause time collector for OpenJDK designed to move us closer to those goals.

Shenandoah trades concurrent cpu cycles and space for pause time improvements. We've added an indirection pointer to every Java object which enables the GC threads to compact the heap while the Java threads are running. Marking and compacting are performed concurrently so we only need to pause the Java threads long enough to scan the thread stacks to find and update the roots of the object graph.

The Shenandoah algorithm is described in depth in this PPPJ2016 paper.

As experimental feature, Shenandoah will require -XX:+UnlockExperimentalVMOptions in the command line. The Shenandoah build system disables the build on unsupported configurations automatically. Downstream builders may choose to disable building Shenandoah with --with-jvm-features=-shenandoahgc on otherwise supported platforms.

To enable/use Shenandoah GC, the following JVM options will be needed: -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC.

 

Java 12 Files mismatch Method Example to Compare two Files

1. Java 12 Files mismatch Overview

In this post, We will learn about new method mismatch() added in Java 12 to Files class. Files class is in package java.nio.file.Files.

java.nio.file.Files class consists exclusively of static methods that operate on files, directories, or other types of files. In most cases, the methods defined here will delegate to the associated file system provider to perform the file operations.

Java 12 API Files mismatch


We will learn how to compare two files using Files.mismatch method.

JDK 12 introduces the new way to determine equality between two files.

Java 12 Collectors.teeing() - Working Examples

1. Overview

In this post, We will learn about new method teeing () added in Java 12 to Collectors class. Collectors class is in package java.util.stream.Collector and it is in java.base module.


Java 12 Collector.teeing​() Method with Example


2. Collectors.teeing ()

Returns a Collector that is a composite of two downstream collectors. Every element passed to the resulting collector is processed by both downstream collectors, then their results are merged using the specified merge function into the final result. 


How to run unix/shell command in java like chmod, mkdir, grep or any unix commands

Running shell command in Java

In this tutorial, We are going to learn how to run shell commands from java such as chmod, mkdir, grep files.

run-shell-command-in-java

Find operating system:

Before running the command, we must know the operating system where the application is running.

By calling System.getProperty("os.name") method, we will get the OS name.

private String OS = System.getProperty("os.name").toLowerCase();

This statement, may return the following operating system names.

Linux, Windows, SunOS, Mac OS, HP-UX and AIX