Java Memory allocation

Source: Internet
Author: User

In general, Java involves the following areas in memory allocation:
Register: we can't control it in the program
Stack: a reference to a primitive type of data and objects, but the object itself is not stored in the stack, but is stored in the heap (new Object)
Heap: storing data generated with new
Static domain: static members that are stored in the object with static definitions
Constant Pool: Storing constants
Non-ram storage: Persistent storage space such as hard drives

Stack: Some of the basic types of variable data defined in the method and reference variables for objects (and formal parameters) are allocated in the stack memory, and local variables in the stack memory disappear as the method Disappears. Data in the existing stack can be shared

Heap: stores the objects and arrays created by new and is managed by the GC. The reference variable is defined in the stack, which is equal to the first address of the array or object in the heap Memory. The reference variable is freed after the program runs outside its scope. Arrays and objects do not, they will only be collected by GC at the appropriate time if no reference variable points to it.

1234567891011121314151617181920212223 Class birthdate {     private int day;      private int month;     private int year;          public birthdate (int d, int m, int y) {          day = d;           month = M;          year = y;     }  }    public class test{     public static  void main (String Args[]) {         int date = 9 ;          test Test = new test ();                test.change (date);       &Nbsp;   birthdate d1= new birthdate (7,7,1970);             }          public  Void change1 (int i) {         i = 1234;      }}

The following analysis of the code execution changes:  
1. The Main method begins execution: int date = 9; Date local variable, underlying type, reference, and value are present in the STACK.  
2. Test test = new test (); Test is an object reference, exists in the stack, and the object (new Test ()) exists in the heap.  
3. test.change (date); i is a local variable, and the reference and value exist in the STACK. When the method change execution is complete, I will disappear from the STACK.  
4. BirthDate d1= New BirthDate (7,7,1970);d 1 is an object reference, exists in the stack, the object (new BirthDate ()) exists in the heap, where D,m,y is stored in the stack as local variables, and their type is the underlying type, So their data is also stored in the STACK. Day,month,year are member variables that are stored in the heap (new BirthDate ()). When the Birthdate construction method finishes executing, the d,m,y disappears from the Stack.
5.main method executes, the date variable, test,d1 reference will disappear from the stack, new test (), new BirthDate () will wait for garbage Collection.

Chang: constant pool refers to 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 primitives and object types defined in the code, there are some symbolic references that appear as text, such as:
The fully qualified name of the class and interface;
The name and descriptor of the field;
The name and descriptor of the method;
The string type is stored in a constant pool when the compilation period has been created (defined directly in double quotation marks) and stored in the heap if the runtime (new) is Determined.


Java 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.