Java Memory management heap memory and stack memory

Source: Internet
Author: User

What we often do is divide the Java memory area into two simple types: heap memory and Stack memory. This division is more coarse grained, and this division is focused on the two types of memory domains that we are most concerned about, which are closely related to object memory allocation. Where stack memory refers to a virtual machine stack, heap memory refers to the Java heap.

1. Stack memory, which is the virtual machine stack. Each method is executed at the same time to create a stack frame, used to store local variables, operation stacks, dynamic links, method exports and other information.local variables include variables of various basic types and reference variables of objects are allocated in the stack memory of the method。 where 64-bit length long and double types of data occupy 2 local variable space, the other data types only occupy 1. The memory space required for a local variable is completed during compilation, and when a method is entered, the local variable space required by this method is determined and will not change during the operation of the method. When a variable is defined in a block of code, Java allocates a memory space for the variable in the stack, and when the scope of the variable is exceeded, Java automatically frees the memory space allocated for that variable, which can be used immediately by another. When a thread requests a stack depth greater than the virtual machine allows, it throwsStackoverflowerrorAbnormal. Thrown when the virtual machine stack cannot be extendedOutOfMemoryErrorAbnormal. The workaround for this scenario is specific to Java tuning. 2. Heap memory, created when the virtual machine is started.the only purpose of heap memory is to create object instances, and all object instances and arrays are allocated on the heap. Heap is responsible for garbage collection, so also called "gc Heap", garbage collection using a generational algorithm, the heap is divided into the new generation and the old age. The advantage of the heap is that the memory can be allocated dynamically, and the lifetime does not have to tell the compiler beforehand because it allocates memory dynamically at runtime, and the Java garbage collector automatically collects the data that is no longer used. However, the disadvantage is that the access speed is slower due to the dynamic allocation of memory at run time. Thrown when heap memory is not expanded because it is fullJava.lang.OutOfMemoryError:Java Heap SpaceAbnormal. The workaround for this scenario is specific to Java tuning.in fact, Java memory is not limited to the above two types, but also:1. The program counter, (in the Thread PC register), records the line number of the bytecode executed by the current thread. 2. The local method stack, similar to the above virtual machine stack, is justThe virtual machine stack is for the Java Method service, and the local method stack is used to serve the native method.3. Method area for storing class information, constants, and static variables that have been loaded by the virtual machine. Garbage collection is less likely to occur in this area. Recycling in this area is the type of uninstallation. The currently loaded type is unloaded only if the class loader instance (non-classloader type) of the type is loaded with the unreachable state. So it is generally not uninstalled.4. Run the constant pool, which holds the various literal and symbolic references generated during the compilation period, which will be stored in the run-time pool of the method area after the class is loaded. The zone is not recycled, for exampleString s= "abc";s= "xyz";S=null;"abc" and "XYZ" will always exist in the string pool, which is one of the reasons why Java memory leaks. excerpted from Http://blog.sina.com.cn/s/blog_6cad92b701019r8o.html

Java Memory management heap memory and stack memory

Related Article

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.