Showing posts with label Jdk8. Show all posts
Showing posts with label Jdk8. Show all posts

Saturday, October 14, 2017

Killing of permgen in Java 8..!!

PermGen  in jdk7 --> Metaspace in jdk8

PermGen is replaced with Metaspace in Oracle/Sun JDK8, which is very similar. The main difference is that Metaspace can expand at runtime.

PermGen Size Limitations :-
  • Limited to MaxPermSize – default ~64M - 85M
  • Contiguous with Java Heap : Identifying young references from old gen and permgen would be more expensive and complicated with a non-contiguous heap – card table(A kind of remembered set that records where oops have changed in a generation).
  • Once exhausted throws OutOfMemoryError "PermGen space".
  • Application could clear references to cause class unloading.
  • Restart with larger MaxPermSize.
  • Size needed depends on number of classes, size of methods, size of constant pools.


Reasons of ignoring these argument is permanent generation has been removed in HotSpot for JDK8 because of following drawbacks
  • Fixed size at startup – difficult to tune.
  • Internal Hotspot types were Java objects : Could move with full GC, opaque, not strongly typed and hard to debug, needed meta-metadata.
  • Simplify full collections : Special iterators for metadata for each collector
  • Want to deallocate class data concurrently and not during GC pause
  • Enable future improvements that were limited by PermGen.
Advantages of MetaSpace :-
  • Take advantage of Java Language Specification property : Classes and associated metadata lifetimes match class loader’s
  • Per loader storage area – Metaspace
  • Linear allocation only
  • No individual reclamation (except for RedefineClasses and class loading failure)
  • No GC scan or compaction
  • No relocation for metaspace objects
Reference https://www.linkedin.com/pulse/killing-permgen-java-8-saral-saxena/

Wednesday, October 11, 2017

Streams in Java 8

1. A stream does not store its elements. They may be stored in an underlying collection or generated on demand.
2. Stream operations don’t mutate their source. Instead, they return new streams.
that hold the result.
3. Stream operations are lazy when possible. This means they are not executed until their result is needed. For example, if you only ask for the first five long words instead of counting them all, then the filter method will stop filtering after the fifth match. As a consequence, you can even have infinite streams.

When you work with streams, you set up a pipeline of operations in three stages.
1. You create a stream.
2. You specify intermediate operations for transforming the initial stream into others, in one or more steps.
3. You apply a terminal operation to produce a result. This operation forces the execution of the lazy operations that precede it. Afterwards, the stream can no longer be used.

NOTE:  Stream operations are not executed on the elements in the order in
which they are invoked on the streams. In our example, nothing happens until count is called. When the count method asks for the first element, then the filter method starts requesting elements, until it finds one that has length > 12.

Working with files

try (Stream<String> lines = Files.lines(path)) {
Do something with lines
}

Below program focus on Stream Creations from different sources:


package com.collection;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.stream.Stream;

public class StreamCreation {
     public static void main(String[] args) {
          String[] tokens = "Its going to be a great Wednesday".split(" ");

          /* Convert an array into Stream */
          Stream<String> fullStream = Stream.of(tokens);
          System.out.println("Arrays to Stream->"+fullStream.count()+"\n"+"******");
         
          /* Convert an array into partial Stream */
          Stream<String> partialStream = Arrays.stream(tokens, 1, 4);
          System.out.println("Arrays to partial Stream->"+partialStream.count()+"\n"+"******");
         
          /* Returns an empty sequential Stream */
          Stream<String> silenceStream = Stream.empty();
          System.out.println("Silence Stream->"+silenceStream.count()+"\n"+"******");

          /* Returns an infinite sequential unordered stream where
           * each element is generated by the provided Supplier
           * Supplier Interface
          */
          System.out.println("Generate Infinite Stream->");
          Stream<String> echos = Stream.generate(() -> "Echo").limit(5);
          echos.forEach(System.out::println);
         
          Stream<Double> randoms = Stream.generate(()->Math.random()).limit(5);
          System.out.println(randoms.count()+"\n"+"******");
         
          /* Returns an infinite sequential ordered Stream produced by iterative application of a
           * function f to an initial element seed, producing a Stream consisting of seed, f(seed),
           * f(f(seed)), etc.
          *  Parameters: seed the initial element
          *  f a function to be applied to to the previous element to produce a new element*/
          Stream<BigInteger> integers=Stream.iterate(BigInteger.ONE,n->n.add(BigInteger.ONE)).limit(5);
          integers.forEach(System.out::println);
     }
}

Output

Arrays to Stream->7
******
Arrays to partial Stream->3
******
Silence Stream->0
******
Generate Infinite Stream->
Echo
Echo
Echo
Echo
Echo
5
******
1
2
3
4

5

Wednesday, November 18, 2015

New Features included in JDK 8 with Examples

1) Introducing LAMDA which are often used in Groovy, Scala(JVM used languages)


/*Arrays class contains various methods for manipulating arrays (such as sorting and searching).
 Arrays class also contains a static factory that allows arrays to be viewed as lists. */

//forEach() -Performs the given action for each element of the Iterable until all elements have been
//processed or the action throws an exception.

import java.util.Arrays;
import java.util.List;

public class Lamda {

public static void main(String[] args) {

// asList converts Arrays to List... Not related to Lamda
List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
System.out.println(stooges);

// by Default Compiler is in command for the list of parameters types
Arrays.asList("a", 11, "d").forEach(e -> System.out.println(e));

// Explicitly providing the type of parameter.. If not match, throws
// Exception
Arrays.asList("a", "Hello", "d").forEach((String e) -> System.out.println(e));

//Lambdas may reference the class members and local variables
String separator = ",";
Arrays.asList("a", "b", "d").forEach((String e) -> System.out.print(e + separator));

//Lamdas may return some value.
Arrays.asList( "a", "b", "d" ).sort( ( e1, e2 ) -> e1.compareTo( e2 ) );
}
}