Java memory management [GO]

Source: Internet
Author: User

First of all, we have to understand that the variable we are using is a piece of memory space!!

first, the principle of memory management:  In Java, there are three levels of Java programs, virtual machines, and operating systems, where Java programs interact with virtual machines, and virtual machines interact with the operating system! This guarantees the platform independence of the Java program! Below we run from the program, the program runs, the program runs memory overflow three stages of memory management principle! 1, the program before running: the JVM to the operating system to request a certain amount of memory space, called the initial memory space! The memory required during program execution is divided by the Java virtual machine from this memory space. 2, program running: Java program has been to the Java Virtual machine to request memory, when the program needs more memory space than the initial memory space, the Java Virtual opportunity to request more memory to the operating system for the program to use! 3, Memory overflow: The program then run, when the Java Virtual machine has requested the memory reached the specified maximum memory space, but the program also needs more memory, then there will be memory overflow error!

As you can see, the memory used by the Java program is managed and allocated by the Java virtual machine. Java Virtual machines prescribe the initial memory space and maximum memory space of a Java program, and developers only need to care about how the Java virtual machine manages the memory space, regardless of how the operating system manages the memory. second, the use of the RUNTIME class:  Java provides us with the runtime class to get information about JVM memory
Method name Parameters Role return value
GetRuntime No Get Runtime Object Runtime Object
TotalMemory No Gets the amount of memory allocated to the program by the JVM Long: Amount of memory
Freememory No Gets the amount of memory currently available Long: Amount of memory
MaxMemory No Gets the maximum amount of memory that the JVM can request Long: Amount of memory
Three, the logical division of memory space:  The JVM divides the requested memory logically into three regions: the method area, the heap, and the stack. Method Area: The default maximum capacity of the method area is 64m,java virtual opportunity to store Java classes loaded into the method area, preserving the structure of the class (properties and methods), class static members, and so on. Heap: The default maximum capacity is 64M, and the heap holds the data held by the object while maintaining a reference to the original class. You can simply understand that the value of the object property is saved in the heap, and the method that the object calls is saved in the method area. Stack: The stack default maximum capacity is 1M, when the program is running, each time a method call is encountered, the Java Virtual machine will be divided into a stack of memory called the stack frame, the stack frame memory for local variables (including the basic type and reference type), when the method call ends, The Java Virtual opportunity reclaims the memory occupied by this stack frame. iv. Java data Types1. Basic data types: variables that do not encapsulate pointers. Declaring this type of variable will only allocate a chunk of memory space in the stack. 2. Reference type: Is the data type of the underlying package pointer. They allocate two pieces of space in memory, the first memory is allocated in the stack, only the other memory address, not the specific value, we also call it pointer type of variable, the second memory allocated in the heap, storage is a specific value, such as object property value. 3. Let's take a look at the following example:
 Public classStudent {String stuid;   String Stuname; intStuage;}  Public classteststudent { Public Static voidMain (string[] args) {Student zhouxingxing=NewStudent (); String name=NewString ("Wang Wang"); intA = 10; Charb = ' m '; Zhouxingxing.stuid= "9527"; Zhouxingxing.stuname= "Week Star"; Zhouxingxing.stuage= 25; } }

(1) The class is, of course, stored in the method area. (2) Student zhouxingxing = new Student (); This line of code created two memory space, the first in the stack, the name is zhouxingxing, it is equivalent to the pointer type of variable, we see it does not hold the student's name, age and other specific values, but instead of storing the second block of memory in the heap address, the second block to store specific values, such as the student's number, Name, age and other information. (3) int a = 10; This is the basic data type variable, the specific value is stored in the stack, and there is no concept of a pointer only! This is the memory layout of this example: In addition we need to know Student zhouxingxing = new Student (); Includes declaration and creation, namely: Student zhouxingxing; and zhouxingxing = new Student (); Where the declaration simply declares a space in the stack, but there is no specific value, The case after the declaration is as follows: The following is created: (4) Arrays in reference types also encapsulate pointers, even arrays of basic data types encapsulate pointers, and arrays are reference types. such as code int[] arr = new int[]{23,2,4,3,1}, as shown in: v. Java values to participate in reference parameters  (1) The parameters are different according to the effect of the call, that is, whether to change the original values of the parameters, but also can be divided into two: parameters passed by value and by reference parameters. The parameter passed by value does not change, the original value of the parameter passed by reference is changed! What is this for? It's quite simple: we know that the variables of the basic data type are stored in the stack, and the variable names are stored in the value of the variable, so when a variable of the basic data type is used as a parameter, the value is passed, just passing the value of the variable, regardless of how the value is manipulated, does not change the variable's original value. In the case of variables of the reference data type, the address at which the variable name resides, so that the reference data type of the variable as a parameter, the actual address is passed, the content at the address of the operation, of course, will change the value of the variable!   (2) Special:string 
 Public class teststring {   publicstaticvoid  main (string[] args) {          = " Wangwang ";      New teststring ();          System.out.println ("before method call:" + name);     Teststring.change (name);     System.out.println ("After method call:" + name);   }       void Change (String str) {     = "Miss Wang";     System.out.println ("After modifying the value in the method body:" + str);   

  Result: Method call before:wangwang  method body modified value: Miss Wang   method call after:wangwang   analysis: The above example, although the parameter string is a reference data type, but its value does not change, This is because the string class is final, it is fixed length, we look at the initial situation, that is, string name = "Wangwang"; This line of code runs out, such as: When the method is called Teststring.change (name), the memory changes to: In the method body, The parameter str is given a new value, str = "Miss Wang". Because string is a fixed length, the system allocates a new memory space of 37DF in the heap, so that Str points to a new memory space of 37DF, and the name is to 36DF, and the 37DF change has no effect on it: Finally, the method call ends, and the memory space of STR and 37DF dies. The value of name is still Wangwang and has not changed. So string is a reference type parameter, but the value remains the same:   (3) cannot be exchanged example: 
public class Testchange {   void change (Student stu1, Student stu2) {     stu1.stuage + +;     Stu2.stuage + +;     Student stu = stu1;     STU1 = STU2;     STU2 = stu;   }      public static void Main (string[] args) {          Student Furong = new Student ();     Furong.stuname = "Sister Furong";     Furong.stuage = +;          Student Fengjie = new Student ();     Fengjie.stuname = "Sister Feng";     Fengjie.stuage = +;          Testchange Testchange = new Testchange ();     Testchange.change (Furong, Fengjie);          System.out.println (furong.stuname);     System.out.println (furong.stuage);          System.out.println (fengjie.stuname);     System.out.println (fengjie.stuage);   }  

  

Operation Result:
Sister Furong 31 Chicken Sister 27 Analysis:

Java memory management [GO]

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.