Heap Overflow
The heap is where Java holds object instances.
Heap overflow can be divided into the following two cases, both of which throw the Outofmemoryerror:java heap space exception:
1. Memory leaks
A memory leak is when an object instance is still referenced after it has been created and used, not released by garbage collection, and accumulates until there is no remaining
Memory is available.
If memory leaks, we want to find out how the compromised object was referenced by GC root, and then use the reference chain to specifically analyze the cause of the leak.
The tools for analyzing memory leaks are: JPROFILER,VISUALVM and so on.
2. Memory Overflow
Memory overflow means that when we create a new strength object, the instance object takes up more memory space than the heap's free space.
If there is a memory overflow problem, this is often the program native needs more memory than we give the virtual machine configuration of memory, in this case, we can use the-XMX to solve this problem.
/**
* @author Z
* Heap Overflow
*/
public class Heap
{
public static void Main (string[] args)
{
ArrayList list=new ArrayList ();
while (true)
{
List.add (New Heap ());
}
}
}
Stack Overflow
Reason
1> function call hierarchy too deep, each call, function parameters, local variables and other information on the stack.
2> local static variable volume is too large
/**
* @author Z
* Stack Overflow
*/
public class Stack
{
public static void Main (string[] args)
{
New Stack (). Test ();
}
public void Test ()
{
Test ();
}
}
Java Basics Overflow