Showing posts with label Example. Show all posts
Showing posts with label Example. Show all posts

Friday, 3 July 2015

Error messages related to Memory Exhausted ( Garbage Collector ) in Java

➟ Purpose

      The main purpose of this article is to understand the root cause behind different kind of error messages we generally face while our day to day coding life. Here, I am going to clear some important and most seen error messages.

➟ Class Hierarchy for Error

➟ Some Examples

➤ java.lang.OutOfMemoryError: GC Overhead limit exceeded

• It does not indicate that heap space memory is 100% full, but it's very small.
• If more than 98% of the total time is spent in garbage collection and less than 2% of the heap is recovered, then this kind of error is thrown.
• This feature is designed to prevent applications from running for an extended period of time while making little or no progress because the heap is too small.
• It can be disable using -XX:-UseGCOverheadLimit

➤ java.lang.OutOfMemoryError: Requested array size exceeds VM limit

• The application (or APIs used by that application) attempted to allocate an array that is larger than the heap size.
• For example, if an application attempts to allocate an array of 512 MB but the maximum heap size is 256 MB.

➤ java.lang.OutOfMemoryError: Java heap space

• Heap space memory is 100% full or it is less than required for allocating to new object.
• This error does not necessarily always imply a memory leak, but it can be.
• The problem can be as simple as a configuration issue, where the specified heap size (or the default size, if it is not specified) is insufficient for the application.

➤ java.lang.OutOfMemoryError: PermGen space

• It indicates the perm gen memory (subpart of heap) is full.
• It indicates it's not able to store more class or method metadata.

• It will be no more thrown while using JDK 8.

➤ java.lang.OutOfMemoryError: Metaspace

• It indicates the native memory (referred to here as metaspace) is full.
• It indicates it's not able to store more class or method metadata.
• It can be thrown while using JDK 8.

➤ java.lang.StackOverflowError

• It indicates the perm gen memory (subpart of heap) is full.
• It indicates it's not able to store more class or method metadata.


How String matters to Garbage Collection and Memory Leak in Java?

➟ What is Memory Leak?

      The application is unintentionally holding references to objects, and this prevents the objects from being garbage collected. This is the Java language equivalent of a memory leak.

➟ What is String Pool?

      String pool (String intern pool) is a special storage area in Java heap. When a string is created and if the string already exists in the pool, the reference of the existing string will be returned, instead of creating a new object and returning its reference.

➤ Some Examples

• Both below are same. It will create new instance only if it is not available in pool, and then add in pool.
String s1 = "Vishal"; 
String s2 = new String("Vishal").intern();

• Below line will create new instance each time.
String s3 = new String("Vishal");

➟ String.substring Method

➤ Before JDK 7 update 6

• String was using counter, offset variable for managing the operations like Substring.
• String was sharing same char[] of original String while calling substring method, thus original String was not eligible for GC.
• Substring method was fast compare to new version.

➤ After JDK 7 update 6

• Counter and offset variables has been removed from String class.
• String is not sharing char[] of original String while calling substring method.
• Substring method is slow compare to old version, because it will create new char[] for each call of substring.

➟ Changes done for String.substring method update in JDK 7u6

➟ Memory Leak Bug Example in before JDK 7u6 (Source)

public class TestGC {
    private String largeString = new String(new byte[100000]);
    
    String getSmallString() {
// if caller stores this substring, this object will not be gc'ed
        return this.largeString.substring(0,2);
//      return new String(this.largeString.substring(0,2)); // no error here!
    }
    
    public static void main(String[] args) {
        java.util.ArrayList list = new java.util.ArrayList();
        for (int i = 0; i < 1000000; i++) {
            TestGC gc = new TestGC();
            list.add(gc.getSmallString());
        }
    }
}
➟ Java 8 Update

➤ String Deduplication :

• A new optimization has been made that enables the G1 collector to identify strings which are duplicated more than once across your heap.
• It will handle it by pointing into the same internal char[] array.
• It will avoid multiple copies of the same string from residing inefficiently within the heap.

Tuesday, 31 December 2013

Layout Example with ExtJS MVC

       Here I am giving very simple but one step complex example compared to my last post hello world example. By reading/understanding this example you can learn real usage of layouts in ExtJS. I am trying to go one by one step for learning ExtJS. Please feel free to give any suggestions if you want to learn about ExtJS.

➟ About the Example

     I am giving brief explanation here about this example. Through this example, I want to provide some layout explanation (H-Box, V-Box, Border etc.) which are very popular and will frequently use in your Web Development. Along with this I also want to provide how to make server/remote call from ExtJS to fetch data. I used here Java-Servlet for fetching data but you can use do server/web-service call for this. I am listing some files in a simple grid which is coming from a server (here apache-tomcat). So here I am trying to integrate Java-EE with ExtJS UI.

➟ Pre-requirements

    To run this example successfully you need to manage these requirements.

     ➤ ExtJS library
  
         You can see about it here.

     Apache-tomcat
 
        You can see about it here.

    ➤ JSON Library for Java
 
        You can see about it here.


➟ Download demo

➟ Snaps

Friday, 22 November 2013

Hello world with ExtJS

    If you didn't download ExtJS library still, please read this. I am trying to give here a very small example in Ext JS. In this example there is one simple form with three fields. I am giving some important explanation about basic and necessary functions or objects of Ext JS.
Ext.onReady
   
    This is the same function like document.onload event in simple HTML. It will call once your html page is loaded. So if we are not following any MVC architecture while developing application using Ext JS, we have to write everything in this function, otherwise it may happen that in which div/body you are trying to render your Ext JS component, it's not rendered on the page.

→ Ext.create

    Whenever you will create a new object (already defined) of any Ext JS component, we will use this function. This function takes two parameters. 1. Full name of the component which you are trying to create as per given by sencha. 2. Parameters or configurations object.

    So now I am giving you my code snap as given below. I hope this will useful for Ext beginner..!!


 
 Hello World
 
 
 





→ Download demo, Live example


→ Snaps

Hello World with Ext JS