Detailed Java Memory Architecture

Detailed Java Memory Architecture

  The memory is divided into Java: stack memory, heap, method area, local area and registers and the like methods.

  The following describes some of the characteristics of the stack memory, the heap memory by each region:

  1, stack memory

  (1) and a variable number of basic types of objects are allocated reference variable stack memory function.

  Data for each stack (2) (primitive types and object references) are private, the other stacks can not be accessed.

  (3) stack is divided into three parts: basic types of variables, and the execution environment context, operation instruction area (storage operation instruction).

  (4) when a variable is defined in a code block, java memory space allocated for the variable in the stack, when the variable exceeds the scope of the

  , Java will automatically freed memory space allocated for the variable, the memory can be immediately used for other purposes.

  (5) When the data has been used less space will be automatically released.

  2, heap memory

  (1) Heap memory used to store objects and arrays created by the new.

  (2) Each entity has a memory address value

  (3) physical variables have default initialization value

  (4) an entity is no longer used, it will be garbage collected in indefinite period of time

  Added: arrays and objects in the absence of reference variables pointing to it, it becomes garbage and can no longer be used, but the memory is still occupied, in a subsequent period of uncertainty is freed garbage collector. This is the main reason java comparison of total memory, in fact, the stack variable to point to heap memory variables, which is a pointer in Java!

  3. The method area

  1. known as a static area, with the same heap, shared by all threads. The method area contains all the class and static variables.

  2. The method area is always included in the only element in the entire program, such as class, static variables.

  The method of a loaded class data storage area information comprises:

  (1) Basic information:

  1) the fully qualified name of each class

  2) direct superclass fully qualified name of each class (type of constraint can be converted)

  3) This class is a class or an interface

  4) The type of access modifiers

  5) an ordered list directly over the fully qualified name of the interface

  Details (2) each loaded class:

  1) runtime constant pool:

  Storing all the constants used by the type of bridge (direct and constant references to other types of symbols, fields, methods), which are accessed through an index in an array, an external contact to call the class of the object and type. It represents a class file is run (bytecodes) during the constant pool. (There is a static constant pool, bytecode file).

  2) field information:

  Information for each field declared in the class (name, type, modifiers).

  3) information:

  Information declared in the class of each method (name, and return the bytecode exception table type, parameter types, modifiers, methods).

  4) static variables

  5) a reference to a class classloader: i.e., to the class of the referenced class loader.

  6) a reference to a class class: create a virtual machine instance for each class type is loaded is used to represent the class is loaded

  Some properties of the above stack memory, heap, the method area, wherein

  Stack has a very important particularity is that there is data in the stack can be shared. Suppose we also define:

  int a = 5;

  int b = 5;

  The compiler first deal with int a = 5; first it will create a reference to a variable on the stack, and then find out whether the value of the stack 5, if not found, it will be 5 stored in, and then a point 5.

  Followed by treatment int b = 5; b After creating the reference variable, as it has the value 5 in the stack, put b directly to 5. Thus, while there have been a and b are the points where 5.

  At this time, if we make a = 8; then the compiler will search again if there are 8 value stack, if not, the 8 stored in, and make a point to 8; If you already have, then this address directly to a point . Thus a change does not affect the value of the value b.

  NOTE: This reference shared data, while the two objects to an object such sharing is different, because such a situation does not affect the modification B, it is done by the compiler, it facilitates save space. And an object reference variables modify the internal state of this object will affect another object reference variables.

  The following example illustrates allocation Java programs in memory:

  Take for example String, String wherein the packaging is a special class data. Can use:

  String str = new String("abc");

  String str = "abc";

  To create two forms, the first one is () to create a new object, it will be stored in a heap in the new. Each call will create a new object.

  While the second is to create a stack object reference variable str String class, and then look for the stack there is no stored "abc", if not, then "abc" stored into the stack, and to make str point "abc" , If you already have "abc" is the direct cause of str point "abc".

  When comparing the class value which are equal, using equals () method; when testing whether two wrapper class reference point to the same object, using ==, the above theory will be described below with examples.

  String str1 = "abc";

  String str2 = "abc";

  System.out.println(str1==str2); //true

  // str1 and str2 can be seen pointing to the same object.

  String str1 =new String ("abc");

  String str2 =new String ("abc");

  System.out.println(str1==str2); // false

  // is a new way to generate different objects. Each one at a time.

  So create multiple "abc" string with the first approach, in fact there is only one object in memory of it. The wording of favorable and save memory space. At the same time it can improve the operating speed of the program to some extent, because the JVM automatically according to the actual situation of the stack data to determine whether it is necessary to create an object.

  For String str = new String ( "abc"); code, flatly create a new object in the heap, regardless of its string value is equal, it is necessary to create new objects, thereby increasing the burden of the program.

  On the other hand, pay attention: We use such as String str = "abc"; the format defined class time, always taken for granted that creates an object str String class object may not beware of traps that may be created.!! just point to an object that has been previously created. Only through the new () method to ensure that each time a new object is created.

Guess you like

Origin www.cnblogs.com/zmy-520131499/p/11128580.html