Java memory area and memory allocation

Source: Internet
Author: User
Tags wrapper

In the recent interview often asked about the JVM and memory allocation problems, I think it is necessary to learn to summarize the next ~ ~ ~One, Java memory areaIn Java, the virtual machine automates memory management, which divides the memory it manages into several different data regions during the Java Virtual machine execution of a Java program, consisting mainly of the following sections:1. Program Counter:The line number indicator of the byte code executed by the current thread; The bytecode interpreter works by changing the value of this counter to select the next byte code instruction to execute. Thread Private: To be able to recover to the correct execution location after a thread switch, each thread needs to have a separate program counter; Java method, the counter records the executingVirtual machine bytecode directiveThe address of the native method, the counter value is empty the only area without outofmemoryerror condition2.Java virtual machine stack: Thread Private, the life cycle is the same as the thread, and the virtual machine stack describes the memory model that the Java method executes.Each method creates a stack frame at execution time (which is actually the space allocated for the virtual machine stack);Exception: Throws a Stackoverflowerror exception when the thread requests a stack depth greater than the virtual machine allows, and the Java Virtual Machine extension cannot request enough memory to throw a OutOfMemoryError exception3. Local method Stack:Using the native keyword for the local method service means that this method is a native function, that is, the method is implemented in C + + language and is compiled into a DLL, which is called by Java.4.Java Heap: thread Sharing, created at virtual machine startup, the only purpose of this memory region is to hold object instances, where almost all object instances are allocated memory, all object instances and arrays are allocated on the heap, Java heap is the main area of GC, is divided into Cenozoic, Laosheng generation, Java heap can be in the physical discontinuous memory space, logical continuity;5. Method AreaA thread-shared area of memory that is used to storeclass information, constants, static variables that have already been loaded, instant compiler compiled code, and so on can choose not to implement garbage collection, garbage collector mainly for the recovery of the constant pool and the type of unloading when the method area does not meet the memory allocation requirements, the OutOfMemoryError exception is thrown ; Run a constant pool: the various literal literals (primitive types) and symbol references that the compiler generates String.intern () can put new constants into the pool at run time;Second, Java memory allocationIn fact, the above has talked about the Java object Memory allocation mechanism, the following details: In Java, everything is considered an object, but the manipulation of the object is actually through the "reference"; in Java, objects created through new are memory-allocated in the heap, and object references are stored in the stack. Basic data type is a special case1. Basic Data TypesThe basic data types are "special" in Java, for example: int a = 1, the variable defined is called an automatic variable, the existence of an automatic variable is a literal value, not an instance of a class, or a reference to a class, a variable is stored in a stack, and a value is stored, not an object reference; about package Type: in Java, 8 basic types are also provided with the corresponding wrapper class, for the wrapper class, you can either use new to create the object, or you can use the basic type of method to directly assign values; Integer Special point: The integer class caches the number between [-128,127], which means that when an integer object is created using an integer a = x, the objects in that range are created directly in the same way as the base type, using the = = When comparing two objects with equal values, the literal is compared directly, so it returns true, and for objects outside that range, it needs to be created in the same way as normal objects, comparing memory addresses when using = =, so two integer objects with the same value will return false;The 8 basic types of Java implement the constant pool technology;2. Reference TypesThe reference type creates a new object instance through new, which allocates memory in the heap and stores a reference to the memory address of the object in the stack;

Java memory area and memory allocation

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.