JDK 8 is a superset of JRE 8, and contains everything that is in JRE 8, plus tools such as the compilers and debuggers necessary for developing applets and applications. JRE 8 provides the libraries, the Java Virtual Machine (JVM), and other components to run applets and applications written in the Java programming language. Note that the JRE includes components not required by the Java SE specification, including both standard and non-standard Java components.
Here are some hightlights of JDK 1.8:
Lambda expressions
Lambda expressions are a new and important feature included in Java SE 8. They provide a clear and simple way to represent one method interface using an expression. Lambda expressions also enhance the Collection libraries making it easier to iterate, filter, and extract data from a Collection.Prior to Java 1.8 anonymous inner classes provided a way to implement callback to events which might occur in an application. Here is for example how a Swing application would create an event handlers to intercept keyboard and mouse events.
JButton testButton = new JButton("Test Button"); testButton.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent ae){ System.out.println("Click Detected by Anon Class"); } });As an alternative, a separate class that implements ActionListener could be used for each event. By creating the class in place, where it is needed, the code is a little easier to read yet the code is not efficient since you needed to create a new class just to define one method:
import java.util.EventListener; public interface ActionListener extends EventListener { public void actionPerformed(ActionEvent e); }Lambda expressions address the bulkiness of anonymous inner classes by converting five lines of code into a single statement. This simple horizontal solution solves the "vertical problem" presented by inner classes.
Now take a look at the following example which used a lambda expression:
public class RunnableTest {
public static void main(String[] args) { System.out.println("=== RunnableTest ==="); // Anonymous Runnable Runnable r1 = new Runnable(){ @Override public void run(){ System.out.println("Hello world one!"); } }; // Lambda Runnable Runnable r2 = () -> System.out.println("Hello world two!"); // Run r1.run(); r2.run(); } }Please notice that in order to use Lambda expressions, you need to download an early access of OpenJDk which includes Lambda Support (JSR 335).
Removal of Permanent Generation
In Java The permanent generation (or permgen) is used for class definitions and associated metadata. Permanent generation is not part of the heap.
The JDK 8 HotSpot JVM is now using native memory for the representation of class metadata called Metaspace; if you have been using Oracle JRockit you will find some of these concepts borrowed from Oracle JVM implementation.
One of the immediate advantages of the Meta Space is that you will forget about the java.lang.OutOfMemoryError: PermGen space problems which is a frequent issue if you try to deploy several times an application on a Container and the Container is unable to free up the space used by previous deployments. Keep in mind however that this new feature won't eliminate out of the box class and classloader memory leaks. You will need however to monitor these problems using a different approach and using different tools.
When running JDK 1.8, allocations for the class metadata will be done out of native memory and the objects used to describe class metadata have been removed.
Another aspect related to Garbage collection is that the collection of the dead classes and classloaders will be done once the class metadata usage reaches the parameter “MaxMetaspaceSize”.
You can monitor metaspace usage by means of the HotSpot 1.8 verbose GC log output. At the time of writing the
Jstat & JVisualVM tools have not been yet updated to monitor the MetaSpace.
So here's how to monitor the Metaspace by adding some Java Options to a Java process:
JAVA_OPTS: "-XX:+UseCompressedOops -Xms64M -Xmx512M -XX:+PrintGCTimeStamps -XX:MaxMetaspaceSize=128m -verbose:gc -XX:+PrintGCDetails -Xloggc:C:/tmp/gc.log
Notice we have not included the -XX:MaxPermSize which is not available anymore and included the -XX:MaxMetaspaceSize which sets the max size for the MetaSpace to 128 MB. When the Threshold for the MetaSpace is approaching, you will see the following log in your Garbage collector dump:
40.134: [Full GC (Metadata GC Threshold) [PSYoungGen: 9189K->0K(32768K)] [ParOldGen: 21476K->27704K(64000K)] 30666K->27704K(96768K), [Metaspace: 52407K->52407K(1099776K)], 0.2789386 secs] [Times: user=0.91 sys=0.00, real=0.28 secs]
Nessun commento:
Posta un commento