-Xmx
, which sets the maximum size of the heap.Tuesday, February 5, 2019
How to deal with “java.lang.OutOfMemoryError: Java heap space” error?
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:
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:
- Implement a clustered web server and/or clustered database server:
- Implement a proxy:
- High Availability for your SQL database
- Finally, to answer your original question about implementing failover at the application level, here are a few examples:
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
, or1MB
. 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,
- Method Area : Storage area for compiled class files. (One per JVM instance)
- Heap : Storage area for Objects. (One per JVM instance)
- Java stack: Storage area for local variables, results of intermediate operations. (One per thread)
- 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)
- Native method stacks : Helps in executing native methods (methods written in languages other than Java). (One per thread)
Explanation of “ClassCastException” in Java?
So, for example, when one tries to cast an
Integer
to a String
, String
is not an subclass of Integer
, so a ClassCastException
will be thrown.Object i = Integer.valueOf(42);
String s = (String)i;
Rethrowing exceptions in Java without losing the stack trace?
catch (WhateverException e) {
throw e;
}
will simply rethrow the exception you've caught (obviously the
surrounding method has to permit this via its signature etc.). The
exception will maintain the original stack trace.Difference between java.lang.RuntimeException and java.lang.Exception
Generally RuntimeExceptions are exceptions that can be prevented programmatically. E.g
EDIT : These days people favor
NullPointerException
, ArrayIndexOutOfBoundException
. If you check for null
before calling any method, NullPointerException
would never occur. Similarly ArrayIndexOutOfBoundException
would never occur if you check the index first. RuntimeException
are not checked by the compiler, so it is clean code.EDIT : These days people favor
RuntimeException
because the clean code it produces. It is totally a personal choice.Keep getting java.lang.NoClassDefFoundError in Spring
The reason for this is, inside your jar you have only your classes without any dependencies.
Java string.split - by multiple character delimiter?
String.split
takes a regular expression, in this case, you want non-word characters (regex \W
) to be the split, so it's simply:String input = "Hi,X How-how are:any you?";
String[] parts = input.split("[\\W]");
How to split a string in Java?
String string = "004-034556";
String[] parts = string.split("-");
String part1 = parts[0]; // 004
String part2 = parts[1]; // 034556
How do I convert a String to an int in Java?
For example, here are two ways:
Integer x = Integer.valueOf(str);
// or
int y = Integer.parseInt(str);
There is a slight difference between these methods: valueOf
returns a new or cached instance ofjava.lang.Integer
parseInt
returns primitiveint
.
What is the difference between Path and ClassPath in Java?
1).Path is an environment variable which is used by the operating system to find the executables.
Classpath is an environment variable which is used by the Java compiler to find the path, of classes.ie in J2EE we give the path of jar files.
2).PATH is nothing but setting up an environment for operating system. Operating System will look in this PATH for executables.
Classpath is nothing but setting up the environment for Java. Java will use to find compiled classes
3).Path refers to the system while classpath refers to the Developing Envornment.
Classpath is an environment variable which is used by the Java compiler to find the path, of classes.ie in J2EE we give the path of jar files.
2).PATH is nothing but setting up an environment for operating system. Operating System will look in this PATH for executables.
Classpath is nothing but setting up the environment for Java. Java will use to find compiled classes
3).Path refers to the system while classpath refers to the Developing Envornment.
Monday, February 4, 2019
Why doesn't the Object already implement Serializable? Members that we wouldn't want to be serializable may be made as transient. Why prevent the default Serializability?
1.
Serializable
is marker interface, which is empty, but when a class is marked Serializable that means its objects are Serializable.Extending class with private constructor in Serializable class
The document says that a non-serializable supertype needs a no-arg
constructor. It does not say it should be private. On the contrary it
says this constructor should be accessible. What the documentation means
about runtime is this
class A {
A() { <-- accessible only in current package
}
}
public class B extends A implements Serializable {
public B() {
}
}
Subscribe to:
Posts (Atom)