default size of Perm space is 64 MB in java.
Showing posts with label error. Show all posts
Showing posts with label error. Show all posts
Wednesday, May 9, 2018
Is it possible to catch out of memory error [OutOfMemoryError] in java?
Yes !! You can catch OutOfMemoryError as it descends from Throwable class.
- public void run() {
- try {
- System.out.println("out of memory error occured");
- } catch (OutOfMemoryError e) {
- System.out.println("Out of memory error handled");
- }
- }
In this case we may not know where the memory leak will happen. thus there is no use of catch block to handle OutOfMemoryError. better increase the heap size by using the flag -Xmx (-Xmx2048M) in project's configuration
- Is it possible to handle exception using OutOfMemoryError in java?
No !! Since, exceptions can be handled using Exception type object not error type object because OutOfMemoryError is error type object. If we really wants to handle any type of error or exception we must make use of Throwable
example:-
- public static void run() {
- try {
- int a = 10 / 0;
- System.out.println("out of memory error occured" + a);
- } catch (OutOfMemoryError e) {
- System.out.println("Out of memory error handled");
- }
- }
Output: Exception in thread "main" java.lang.ArithmeticException: / by zero
- public static void main(String[] args) {
- run();
- }
- public static void run() {
- try {
- int a = 10 / 0;
- System.out.println("out of memory error occured" + a);
- } catch (Throwable e) {
- System.out.println("Out of memory error handled");
- }
- }
Output: Out of memory error handled
How do you deal with OutOfMemoryError in java?
Problem statement: what is OutOfMemoryError and how to deal with OutOfMemmoryError?
OutOfMemoryError is thrown when there is insufficient space to allocate an object in java heap. In this case, garbage collector cannot make space available for new object and and thus it cause an error. It is said this is a serious problem and an application should not try to catch this error, rather size should be increased for JVM by using a flag named -Xmx (e.g. -Xmx1024M)
OutOfMemoryError is thrown when there is insufficient space to allocate an object in java heap. In this case, garbage collector cannot make space available for new object and and thus it cause an error. It is said this is a serious problem and an application should not try to catch this error, rather size should be increased for JVM by using a flag named -Xmx (e.g. -Xmx1024M)
- How to solve OutOfMemoryError in java?
There are two types of OutOfMemoryError in java
- java.lang.OutOfMemoryError: Java heap space
- java.lang.OutOfMemoryError: PermGen space
- It can be solved by increasing the size of Java heap space and PermGen space using flag -Xmx and -XX:MaxPermSize
- An important point to remember is that it doesn't depend on –Xmx value so no matter how big your total heap size you can run OutOfMemoryError in perm space.
- The good thing is you can specify the size of permanent generation using JVM options "-XX: PermSize" and "-XX: MaxPermSize" based on your project need.
- One small thing to remember is that "=" is used to separate parameter and value while specifying the size of perm space in the heap "=" is not required while setting maximum heap size in java it is required, as shown in below example.
export JVM_ARGS="-Xmx1024m -XX:MaxPermSize=256m"
How do you deal with StackOverflowError in java?
Problem statement: what is StackOverflowError and how do you deal with this ?
- Constructor of StackOverflowError class:
- StackOverflowError():creates/constructs an object of StackOverflowError class, with no details message.
- StackOverflowError(String s):creates/constructs an object of StackOverflowError class, with the specified detail message.
- Hierarchies of StackOverflowError class:
- StackOverflowError extends VirtualMachineError: it indicates JVM is broken
- VirtualMachineError extends Error: it indicates serious problem that an application should not try to catch.
- Error extends Throwable and furthermore Throwable extends Object.
- What is StackOverflowError: Stack overflow means exactly - a stack overflows. Usually there's a one stack in the program that contains local-scope variables and addresses where to return when execution of a programs ends. That stack tends to be a fixed memory range somewhere in the memory, therefore it's limited how much it can contain values.
- If the stack is empty you can't pop, if you do you'll get stack underflow error.
- If the stack is full you can't push, if you do you'll get stack overflow error.
- So stack overflow appears where you allocate too much into the stack.
- what is the cause of StackOverflowError ?
- when recursive function does not have the correct termination condition, then it ends up calling itself forever and leads to StackOverflowError.
- If you are calling library function that indirectly cause function to be called.
- How do you deal with StackOverflowError?
- Inspect the stack trace and detect the repeating pattern of line numbers.
- these line numbers indicates code being recursively called.
- once you detect these lines, inspect your code and understand why recursion never terminates.
- if code is terminating the recursion correctly, then increase the thread stack's size, in order to allow large number of invocations.
- default thread stack size is equal to 1MB.
- thread stack size can be increase using -Xss flag
- -Xss flag can be specified either via project's configuration, or via command line.
- value can be set like -Xss2048k or -Xss2M don't use = operator like -Xss=2048k
- public class StackOverFlowErrorExe {
- public static void recursiveCall(int num) {
- System.out.println("Number: " + num);
- if (num == 0) {
- return;
- }else {
- recursiveCall(++num);
- }
- }
- public static void main(String[] args) {
- StackOverFlowErrorExe.recursiveCall(1);
- }
- }
Output:
Exception
in
thread
"main"
java.lang.StackOverflowError.
- How to configure -Xss flag in eclipse:
Subscribe to:
Posts (Atom)
How to run standalone mock server on local laptop
Please download the standalone wiremock server from Direct download section at the bottom of the page. Download and installation Feel fre...
-
Problem statement: what would be the better option to choose between char[] array and String. Answer: Character array i.e. char[] over ...
-
अपनी धुन में रहता हूँ मैं भी तेरे जैसा हूँ ओ पिछली रुत के साथी अब के बरस मैं तनहा हूँ अपनी धुन में... तेरी गली में सारा दिन दुख के कंकर ...
-
Please download the standalone wiremock server from Direct download section at the bottom of the page. Download and installation Feel fre...