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 example: java -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.