How to Solve JVM Common Errors: outofmemoryerror & stackoverflowerror

OutOfMemoryError

Error cause: java.lang.outofmemoryerror: Java heap space heap memory overflow
solution: adjust the size of the heap memory

// -Xms1m -Xmx10m -XX:+PrintGCDetails
		List<Object> listObject = new ArrayList<>();
		for (int i = 0; i < 10; i++) {
			System.out.println("i:" + i);
			Byte[] bytes = new Byte[1 * 1024 * 1024];
			listObject.add(bytes);
		}
		System.out.println("Added successfully...");

StackOverflowError

Cause of error: java.lang.stackoverflowerror is expressed as stack overflow, which generally occurs in recursive calls
solution: set the maximum thread call depth, which is 1m by default

//-Xss5m Set the maximum call depth
public class StackTest {
	private static int count;
	public static void count(){
		try {
			count++;
			count(); 
		} catch (Throwable e) {
			System.out.println("the maximum depth:"+count);
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
			 count();
	}
}

Read More: