Java Memory allocation

Source: Internet
Author: User

This article is reproduced from Elsewhere, specifically which is written by the great God do not know;

Stack, heap, Chang, although the same is the Java memory allocation operation area, but its adaptability and function is very different. Java memory allocation and management is one of the core technologies of Java, the general Java in memory allocation will involve the following areas:
    • Register: No Control in the program (supplemental: c is the Register keyword can be assigned to the data on the register)
    • Stack: A reference to a primitive type of data and objects, but the object itself is not placed in the stack, but in the heap
    • Heap: Storing the data generated by new
    • Static domain: holds static members defined with static in an object
    • Chang: Storing constants
    • Non-RAM storage: Persistent storage space such as hard drives
Stacks in Java memory allocations some basic types of variable data and object reference variables defined in the function are allocated in the stack memory of the function when a variable is defined in a block of code, Java allocates memory space for the variable in the stack, and when the variable exits the scope, Java automatically frees up the allocated memory space for the variable, which can be immediately used by another heap heap memory allocated by Java memory to hold the object and array created by new. The memory allocated in the heap is managed by the automatic garbage collector of the Java Virtual machine. After creating an object or an array in the heap, you can also define a special variable in the stack that is equal to the first address of the array or object in the heap memory, and this variable in the stack becomes the reference variable of the array or object. The reference variable is equal to the name of the array or object, and the reference variable in the stack can then be used in the program to access the arrays or objects in the heap. A reference variable is equivalent to a name that is an array or an object a reference variable is a normal variable that is allocated in the stack when defined, and the reference variable is freed after the program runs outside its scope. While the array and the object itself are allocated in the heap, the memory occupied by the array and the object itself is not freed, even if the program runs outside of the code block that contains the statement that generated the array or object, and the array and object become garbage when no reference variable points to it, not in use, but still occupy the memory space. In a subsequent uncertain time the garbage collector takes away (released). This is why the Java comparison accounts for memory. The Chang constant pool refers to some of the data that is determined at compile time and is saved in the compiled. class file. In addition to the constant values (final) that contain the various basic data types and object types (strings and arrays) defined in the code, there are some symbolic references that appear as text, such as:
    • Fully qualified names for classes and interfaces
    • Name and descriptor of the field
    • Methods and names and descriptors
  Virtual machines must maintain a constant pool on each mounted type. A constant pool is an ordered set of constants used by that type, including direct constants (string, Integer, and so on) and other types, symbol references for fields and methods   for string constants, its value is in a constant pool. The Chang in the JVM exists as a table in memory, and for string types there is a fixed-length constant_string_info table for storing file string values, note that the table stores only literal string values and does not store symbol references. In this case, the storage location of the string values in the constant pool should have a relatively clear understanding of the   when the program executes, the constant pool exists in method area, not in the heap. The heap and stack Java heap is a run-time data area from which the class's objects allocate space. These objects are established through directives such as New,newarry,anewarray and Multianewarray, and they do not require program code to display the release. Heap is responsible for garbage collection, the advantage of the heap is the ability to dynamically allocate memory size, the lifetime does not have to tell the editor beforehand, because it is at runtime to allocate memory dynamically, Java garbage collector will automatically take away these no longer use data. However, the disadvantage is that due to the dynamic allocation of memory at run time, the advantage of slower access   stack is that the access speed is faster than the heap, second only to register, the stack data can be shared. However, the disadvantage is that the size and lifetime of the data in the stack must be deterministic and inflexible. The stack contains some basic types of variable data (byte, short, int, long, char, float, double, Boolean) and object handle (reference)   stack has a very important particularity, is that there is data in the stack can be shared. Suppose we define at the same time: int a=3;int b=3; the compiler processes int a = 3 first; First, it creates a reference to a variable in the stack, and then looks for a value of 3 in the stack, if not found, stores 3 in, and then points a to 3. int B = 3 is processed, and after the reference variable of B is created, because there are already 3 values in the stack, Point B directly to 3. In this way, A and B are pointing to 3 at the same time   then if a = 4, then the compiler will re-search the stack for 4 values, if not, the 4 is stored in, and a point to 4, if already, then direct a to the address. So a change in the value of a will not affect the value of B   be aware that this sharing of data with two object references while pointing to an object is different. Because a modification of this condition does not affect B, it is done by the compiler, which facilitates the saving of emptyRoom An object reference variable modifies the internal state of the object, affecting another object reference variable. String

String is a special wrapper class data that can be used:

String str = new String ("abc");

String str = "ABC";

In both forms, the first is to create new objects with new (), which is stored in the heap. A new object is created each time the call is made. The second is to create an object reference variable str to the String class in the stack now, and then use the symbol reference to go to the string constant pool to find "ABC", if not, put "ABC" into the string constant pool and point str to "ABC" if there is already "ABC" The STR directly points to "ABC". Compare the values in the class with Equals () method; When testing two wrapper classes for reference to the same object, use = =, the following example illustrates the above theory.
      1. String str1 = "abc";
      2. String str2 = "abc";
      3. System.out.println (str1 = = str2); //True
You can see that str1 and str2 point to the same object
    1. String str3 = new String ("abc");
    2. String STR4 = new String ("abc");
    3. System.out.println (STR3 = = STR4); //False

The new method is to generate different objects in the heap because of the immutable nature of the string class, you should consider using the StringBuffer class when the string variable needs to change its value frequently to improve program efficiency
    1. The string class is not part of 8 basic data types, and string is an object. Because the default value of the object is NULL, the default value of string is also null. But it is a special kind of object, there are some features that other objects do not have
    2. New String () and new String ("") are all declarations of a new empty string, that is, the empty string is not NULL,
A reference to the local variable data and objects that are used to hold some raw data types in the stack, but not the object's content heap. The object string created with the New keyword is a special wrapper class whose reference is stored in the stack, and the object content must be determined differently (Chang and heap) depending on how it was created. Some compile time has been created, stored in the string constant pool, and some run time is created. Use the New keyword to store in the heap

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.