If you find this page useful, please consider making a quick and secure donation (powered by PayPal) to keep it free!
 
 

Knowledge Base/Java/General

 

From The Thalesians

Jump to: navigation, search

How to deal with java.lang.OutOfMemoryError: Java heap space

To increase the heap space, pass the following parameters to the JRE:

java -Xms524288 - Xmx1073741824

-Xms specifies the initial Java heap size. -Xmx specifies the maximum Java heap size. The values supplied are in bytes. If the -Xmx is exceeded, an OutOfMemoryError will be thrown.

Stack trace as a String

The printStackTrace() method of Throwable (a parent of Exception) will send the stack trace to System.err. As of Java 1.4, getStackTrace() will return the stack trace as StackTraceElement[]. But what if a simple String is needed?

The easiest thing to do is to use yet another method, printStackTrace(PrintStream s), and convert its output to a String, as follows:

  1. public static String getStackTrace(Throwable t) {
  2. final Writer result = new StringWriter();
  3. final PrintWriter printWriter = new PrintWriter(result);
  4. t.printStackTrace(printWriter);
  5. return result.toString();
  6. }
 
 
Google
 
Personal tools