Tuesday, February 5, 2019

How to deal with “java.lang.OutOfMemoryError: Java heap space” error?


Run Java with the command-line option -Xmx, which sets the maximum size of the heap.

how to increase java heap memory permanently?


I have one problem with java heap memory. I developed one client server application in java which is run as a windows service it requires more than 512MB of memory. I have 2GB of RAM but when I run my application it throws an exception

Out of memory error:java heap space

but I have already set heap size (maximum 512MB) in the java control panel and I still get the same error. I can't set heap size through the command line because my application runs as a windows service so how can I increase the default heap size?

Answer : - 
The Java Virtual Machine takes two command line arguments which set the initial and maximum heap sizes: -Xms and -Xmx. You can add a system environment variable named _JAVA_OPTIONS, and set the heap size values there.
For example if you want a 512Mb initial and 1024Mb maximum heap size you could use:
under Windows:
SET _JAVA_OPTIONS = -Xms512m -Xmx1024m
under Linux:
export _JAVA_OPTIONS="-Xms512m -Xmx1024m"
It is possible to read the default JVM heap size programmatically by using totalMemory() method of Runtime class. Use following code to read JVM heap size.
public class GetHeapSize {
    public static void main(String[]args){

        //Get the jvm heap size.
        long heapSize = Runtime.getRuntime().totalMemory();

        //Print the jvm heap size.
        System.out.println("Heap Size = " + heapSize);
    }
}

 

I am wondering if I have 2 servers, one that is "main" server and the other one is just there(Both are using the same back-end code). How should I handle if main server crash and I want my clients to user the other server, making it so they don't have to restart their program or anything like that?

Here are some alternatives that might be much more robust:
ADDENDUM:

java.lang.StackOverflowError – How to solve StackOverflowError

  • The simplest solution is to carefully inspect the stack trace and detect the repeating pattern of line numbers. These line numbers indicate the code being recursively called. Once you detect these lines, you must carefully inspect your code and understand why the recursion never terminates.
  • If you have verified that the recursion is implemented correctly, you can increase the stack’s size, in order to allow a larger number of invocations. Depending on the Java Virtual Machine (JVM) installed, the default thread stack size may equal to either 512KB, or 1MB. You can increase the thread stack size using the -Xss flag. This flag can be specified either via the project’s configuration, or via the command line. The format of the -Xss argument is:
    -Xss<size>[g|G|m|M|k|K]

How to handle StackOverflowError in Java?

public class Example {
    public static void endless() {
        endless();
    }

    public static void main(String args[]) {
        try {
            endless();
        } catch(StackOverflowError t) {
            // more general: catch(Error t)
            // anything: catch(Throwable t)
            System.out.println("Caught "+t);
            t.printStackTrace();
        }
        System.out.println("After the error...");
    }
}

Where does class, object, reference variable get stored in java. IN heap or stack? Where is heap or stack located?

Runtime data area in JVM can be divided as below,
  1. Method Area : Storage area for compiled class files. (One per JVM instance)
  2. Heap : Storage area for Objects. (One per JVM instance)
  3. Java stack: Storage area for local variables, results of intermediate operations. (One per thread)
  4. PC Register : Stores the address of the next instruction to be executed if the next instruction is native method then the value in pc register will be undefined. (One per thread)
  5. Native method stacks : Helps in executing native methods (methods written in languages other than Java). (One per thread)

What is the purpose of rethrowing the same exception in Java?