java stack overflow and heap overflow

With basic types is not instantiated, direct initialization, assignment, calculation. The method call is not allowed into the container (claim must be a class instance of the job).
Package type is the base type into a class instance, it must produce new, can invoke methods, it can be placed into a container.
You must first figure out what to put in the stack:

Variables declared runtime stack stores - object reference (or base type, primitive) memory, each allocated heap target content (example) memory.

Has a variable can point to multiple object instances; arrays, linked lists, etc. can be stored in a reference to the relationship between multiple instances of the object content. Object content does not reference the relationship is said to be a virtual machine recovery (recycled, destroy, in C ++ called delete, in C called free). The stack is implemented after the first, and that similar cargo containers. The heap is randomly stored, similar to the current parking lot. Remember, book a foreign game, said game space stack and heap space to be allocated good; allocate space for a fixed capacity at the beginning of time, have a different distribution of top-down and bottom-up address space.

stackoverflow, always infinite recursive calls when you can see (google often have the same name as the result of the site).

Heap memory is full, it can be achieved through unlimited new.

================================

In JAVA, you can use the new keyword to create Java objects. E.g,

ArrayList list = new ArrayList();

In fact, after you create an object above, in the JVM, put out new objects stored in the heap memory,

Meanwhile, the storage of objects in the stack method referenced relations.

If you want to heap overflows, relatively simple, it can be recycled to create objects or large objects;

If you want to stack overflow can be called recursively methods, such as stack depth increases, JVM maintains a long track method calls, until enough memory allocation, resulting in a stack overflow. Thus, the following codes may be used to achieve a simple stack overflow and stack overflow.

public class Test {
    public void testHeap(){
        for(;;){
              ArrayList list = new ArrayList (2000);
          }
    }
    int num=1;
    public void testStack(){
        num++;
        this.testStack();
     }
    
    public static void main(String[] args){
        Test  t  = new Test ();
        t.testHeap();
        t.testStack();   
    }
}

In addition, how the Java Virtual Machine heap size settings: Command-line

java -Xms128m // JVM takes up minimal memory

       –Xmx512m //JVM占用最大内存

       –XX:PermSize=64m //最小堆大小

       –XX:MaxPermSize=128m //最大堆大小
发布了15 篇原创文章 · 获赞 0 · 访问量 433

Guess you like

Origin blog.csdn.net/hblack_313/article/details/104256671