1. The simplest memory overflow
public class Heapoom { heapoom[] testlist= new heapoom[100000]; public static void Main (string[] args) { list
2, virtual machine stack and local method stack Overflow (-XSS: stack memory capacity)
Two kinds of exceptions:
A Stackoverflowerror exception is thrown if the thread requests a depth greater than the maximum allowed depth for the virtual machine.
Throws a OutOfMemoryError exception if the virtual machine cannot request enough memory space on the scale stack.
Single Thread time stack overflow:
public class StackOverflow { private int stacklength = 1; public void Stackleak () { stacklength++; Stackleak (); } public static void Main (string[] args) throws Throwable { StackOverflow stackoverflowerror = new StackOverflow ();
try { stackoverflowerror.stackleak (); } catch (Throwable e) { System.out.println ("Stack depth:" +stackoverflowerror.stacklength); throw e;}} }
Stack overflow under multi-threading:
A memory overflow exception can be generated by constantly creating threads. In this case, the larger the memory allocated for each thread's stack, the more likely it is to produce a memory overflow exception.
The reason is that there is a limit to the memory assigned to each process by the operating system, such as 32-bit Windows limited to 2GB. The virtual machine provides parameters to control the maximum value of both the Java heap and the two-part memory of the method area. The remaining memory is 2GB minus XMX (maximum heap capacity), minus maxpermsize (maximum method area capacity)
public class Stackoverthread { private void Donnotstop () { while (true) { donnotstop () } } public void Stackleakbythread () {if (true) { thread thread = new Thread (new Runnable () {public void run () { donnotstop (); } }); Thread.Start (); } } public static void Main (string[] args) { Stackoverthread stackoverthread = new Stackoverthread (); Stackoverthread.stackleakbythread (); }}
3.
Common error in Java Virtual machine