Wednesday, November 18, 2015

Java: Heap Size, Stack Size and Perm Gen / Metaspace Size

A. What are heap, stack and perm gen?


Heap is the place in the memory stores the objects created by your application. This is where garbage collection starts.

Stack is the place in the memory stores the threads generated by your application.

Permanent Generation, also known as Perm Gen or Perm, is the place in memory stores class definitions and static fields. Stuffs stored here are generally not garbage collected.

 Insufficient memory in any of the three places will cause java.lang.OutOfMemoryError. However, in java 8, the Perm Gen is replaced by Metaspace which eliminates the corresponding OutOfMemoryError unless the Metaspace is limited on purpose.

B. Set the size of heap, stack and perm gen


Heap:  use the following parameters to set the heap sizes
 
           -Xms<minSize>  for the initial heap size
          - Xmx<maxSize> for the maximum heap size

          for example: java -Xms512m -Xms2048m MyMagicPerformer.class

          For setting them in NetBeans, right click on your executable project, open the properties window, click run in the left Categories list, on the right side of the window, enter -Xms<minSize> -Xmx<maxSize> in the VM Options.

Stack: use the following parameter to set the stack sizes

          -Xss<size>

           for example: java -Xss1024k MyMagicPerformer.class

Perm Gen: use the following parameters to set the perm sizes

           -XX:PermSize=<size>  for the initial perm size
          -XX:MaxPermSize=<size>  for the maximum perm size

          for examplejava -XX:PermSize=64m -XX:MaxPermSize=128m MyMagicPerformer.class

          In java 8, the Perm Gen is replace by Metadata. By default the Metadata allocation is limited by the amount of available native memory. A new flag MaxMetaspaceSize can be used to limit the amount of native memory used for class metadata. If you don’t specify this flag, the Metaspace will dynamically re-size depending of the application demand at runtime

           -XX:MaxMetaspaceSize=<size>
          java -XX:MaxMetaspaceSize=128m MyMagicPerformer.class

C. Find out the sizes


            On Linux/Unix and Mac OSX: 
            java -XX:+PrintFlagsFinal -version | grep -iE 'HeapSize|PermSize|ThreadStackSize'

            If you would like to find out the memory sizes of a particular java program, replace the "-version" argument with the name of your java program. For example: java -XX:+PrintFlagsFinal MyMagicPerformer | grep -iE 'HeapSize|PermSize|ThreadStackSize'

           On Windows:
           java -XX:+PrintFlagsFinal -version | findstr /i "HeapSize PermSize ThreadStackSize"
-----------------------------------------------------------------------------------------------
                        

If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.
    

Friday, November 6, 2015

java.net.UnknownHostException

If you are able to ping the IP address, but not able to ping the domain name, for example,

> ping 216.58.219.228
PING 216.58.219.228 (216.58.219.228) 56(84) bytes of data.
64 bytes from 216.58.219.228: icmp_seq=1 ttl=53 time=33.0 ms
64 bytes from 216.58.219.228: icmp_seq=2 ttl=53 time=32.9 ms

> ping www.google.com
ping: unknown host www.google.com

you need to update your domain name server (DNS) to add the entry for www.google.com, then update the /etc/resolv.conf file by adding the following line.

nameserver       <DNS IP address>

Or you can directly add the following line to your /etc/resolv.conf file.

www.google.com     216.58.219.228


If you cannot reach the host by pinging either the domain name or the IP address, you need to establish the connection between the host and your machine first.

-------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one?Read the book free here.