Java Basics Overflow

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.