Wednesday, January 16, 2019

Java 8 - Functional Interfaces


Java functional interfaces use cases :





Primitive specializations



Method reference receipt:




Reference :  Modern Java In action book

Friday, January 11, 2019

Java performance tuning

Articles

1. Simple tuning : link

2. How to write memory efficient code - Youtube video link

Notes:

* Check the current log level first
This recommendation should be obvious, but unfortunately, you can find lots of code that ignores it. Before you create a debug message, you should always check the current log level first. Otherwise, you might create a String with your log message that will be ignored afterward.

Java throubleshooting

Articles

1. Types of profiles : link , Youtube video

2. How to get high CPU usage in java(thread dump) : link 

3. Heap dump analysis, Youtube video : link

4. Article for OutOfMemmoryError : link

Some useful commands
- jps
- jmap -histo:[live] PID -> for historgram
- jmap dump : filename PID -> heap dump
  example: jmap -dump:live,format=b,file=/tmp/dump.hprof
- XX:HeapDumpOutOfMemoryError and XX:HeapDumpFileLocation $some_path

Wednesday, January 2, 2019

String basic formulas

String Basics

string is traditionally a sequence of characters, either as a literal constant or as some kind of
variable. The latter may allow its elements to be mutated and the length changed, or it may be
fixed (after creation).
Some languages provide strings as a builtin datatype ( Like C++ , Java , C# ) whereas some
implements string as an array of characters ( Like C ).
Strings are not available in C instead, we use a char array to read strings, where the end of string
is marked with the special character \0 often called as null character.
When we have a char arr[] in C and want to iterate over the characters with a loop like
for (int i = 0;i < strlen(arr);i++){ 
    printf("%c",arr[i]) ;
}    
we have to be careful because by using strlen(arr), the complexity of the operation goes
unknowingly up to . That is because strlen(arr) is a  operation in itself, so it should not be used
in the termination condition.
int len = strlen(arr) ;
for (int i = 0;i < len;i++){ 
    printf("%c",arr[i]) ;
}

Substring:

A substring is a part of string  such that . It is a contiguous slice of the original string.
For example : List of substrings of string S = "abc" contains following strings.
  1. a
  2. b
  3. c
  4. ab
  5. bc
  6. abc
Therefore, a string of length  contains  substrings.

Subsequence:

A subsequence is a sequence that can be derived from another sequence by deleting some
elements ( possibly zero but not all ) without changing the order of the remaining elements.
For example : List of subsequences of string S = "abc" contains following sequences.
  1. a
  2. b
  3. c
  4. ab
  5. bc
  6. ac
  7. abc
Therefore, a sequence of size  contains  subsequences.

Subset:

Subset is any unordered set of elements from the original list.
For example : List of subsets of string S = "abc" contains following sets.
  1. {}
  2. {a}
  3. {b}
  4. {c}
  5. {c,b}
  6. {a,b}
  7. {a,c}
  8. {a,b,c}
Therefore, a set of size  contains  subsets.
NOTE :
  1. {b,a,c} is a subset of string "abc" but not a subsequence.
  2. Each subsequence of a collection of elements is its subset also, but reverse does not hold.

Sublist:

Sublist is any unordered list derived from the original list. Here elements need not be unique,
but should exist on separate indices in the original list.

Source : Hackerrank.com

Tuesday, December 25, 2018

JVM architecture

Understanding JVM internals

Articles:
1. Introduction level : Geeks4geeks and Dzone
2. Little bit deep : Understanding JVM internals
3. JVM specifications : JSE 8

Lessons learned 

Stack-based virtual machine: The most popular computer architectures such as Intel x86 Architecture and ARM Architecture run based on a register. However, JVM runs based on a stack

Symbolic reference: All types (class and interface) except for primitive data types are referred to through symbolic reference, instead of through explicit memory address-based reference.

Network byte order: The Java class file uses the network byte order. To maintain platform independence between the little endian used by Intel x86 Architecture and the big endian used by the RISC Series Architecture, a fixed byte order must be kept. Therefore, JVM uses the network byte order, which is used for network transfer. The network byte order is the big endian.

* The class file itself is a binary file that cannot be understood by a human. To manage this file, JVM vendors provide javap, the disassembler. The result of using javap is called Java assembly.

* JVM follow Delegation-Hierarchy principle to load classes. System class loader delegate load request to extension class loader and extension class loader delegate request to boot-strap class loader. If class found in boot-strap path, class is loaded otherwise request again transfers to extension class loader and then to system class loader. At last if system class loader fails to load class, then we get run-time exception java.lang.ClassNotFoundException.

*The method area can be implemented in various formats by JVM vendor. Oracle Hotspot JVM calls it Permanent Area or Permanent Generation (PermGen). The garbage collection for the method area is optional for each JVM vendor.

* The bytecode that is assigned to the runtime data areas in the JVM via class loader is executed by the execution engine. The execution engine reads the Java Bytecode in the unit of instruction. It is like a CPU executing the machine command one by one

* Interpreter: Reads, interprets and executes the bytecode instructions one by one. As it interprets and executes instructions one by one, it can quickly interpret one bytecode, but slowly executes the interpreted result. This is the disadvantage of the interpret language. The 'language' called Bytecode basically runs like an interpreter.

* JIT (Just-In-Time) compiler: The JIT compiler has been introduced to compensate for the disadvantages of the interpreter. The execution engine runs as an interpreter first, and at the appropriate time, the JIT compiler compiles the entire bytecode to change it to native code. After that, the execution engine no longer interprets the method, but directly executes using native code. Execution in native code is much faster than interpreting instructions one by one. The compiled code can be executed quickly since the native code is stored in the cache.

*It takes more time for JIT compiler to compile the code than for the interpreter to interpret the code one by one. Therefore, if the code is to be executed just once, it is better to interpret it instead of compiling. Therefore, the JVMs that use the JIT compiler internally check how frequently the method is executed and compile the method only when the frequency is higher than a certain level.

* The Hotspot VM is divided into the Server VM and the Client VM, and the two VMs use different JIT compilers.