Java advanced-object and Memory Control (1)

Source: Internet
Author: User

Java advanced --- object and Memory Control (1)

Java provides an excellent garbage collection mechanism to recycle allocated memory, but this does not mean that we can squander the memory allocation of Java programs in the process of programming, this will cause low program running efficiency and directly affect the overall user experience of the program.

Java memory management mechanisms include memory allocation and memory recovery. The memory allocation mechanism allocates the specified memory space in the heap memory when the Java object is created. Memory recycle refers to when the Java object is out of reference and becomes garbage, the memory recycle mechanism will automatically clear the object and reclaim the memory occupied by the object.

Java's memory collection mechanism is completed by a background thread, which consumes a lot of performance. Therefore, if you create objects without authorization, the system can allocate memory, the allocated memory will be recycled by the garbage collection mechanism, which will not only reduce the available memory in the system, reduce the program running performance, but also increase the garbage collection burden, reduces Program Running Performance.

1. instance variables and class variables

Java program variables can be divided into member variables and local variables. Local variables can be divided into three types:

> Parameter: A local variable defined in a method signature. It is assigned by the method caller and disappears with the completion of the method;

> Local variables in a method: the local variables must be displayed and initialized in the method. The local variables take effect after the initialization is complete and disappear with the completion of the method;

> Local variables in a code block: they must be displayed and initialized in the code block. The local variables take effect after initialization and die as the code block ends.

Local variables are defined in the method signature, method, and code block. Stored in method stack memory.

A member variable is a variable defined in the class body and stored in the class Stack. If the static modification is not used when defining the member variable, the member variable is also called a non-static variable or an instance variable. Otherwise, it is called a static variable or a class variable.

1) attributes of instance variables and class variables

The class variable belongs to the class, and the instance variable belongs to the class instance. In the same JVM, each class corresponds to only one class object, but each class can create multiple Java objects.

Because each class in the same JVM only corresponds to one class object, class variables in one JVM only need one memory space. However, for instance variables, each time an instance is created for this class, a memory space needs to be allocated for the instance variable. That is to say, there are several instances in the program, and the instance variable requires several pieces of memory space.

The following program can be used to better understand instance variables and class variables:

 

Class nofive {string name; int ID; static int age; Public void output () {system. out. println ("name:" + name + ", student ID:" + id) ;}} public class test01 {public static void main (string [] ARGs) {// The class variable belongs to the class itself. Once the class initialization is complete, the program can use the class variable nofive. age = 22; // use the class variables system. out. println ("the age of nofive is:" + nofive. age); nofive = new nofive (); nofive. name = "Xiao Wu Ling"; nofive. id = 41009160; nofive. age = 22; // use nofive to access the age class Variable System of the nofive class. out. println ("Access age class variables through nofive variables:" + nofive. age); nofive. output (); nofive nofive2 = new nofive (); nofive2.name = "Ling xiaowu"; nofive2.id = 6190014; nofive2.age = 23; nofive2.output (); // use nofive2 to modify the age variable nofive2.age = 21 of the nofive class; // access the age variable system of the nofive class through nofive, nofive2, and nofive respectively. out. println ("Access age class variables through nofive variables:" + nofive. age); system. out. println ("Access age class variables through the nofive2 variable:" + nofive2.age); system. out. println ("Access age class variables through nofive class:" + nofive. age );}}

 

2) initialization time of instance variables

The program is running. Every time a Java object is created, the memory space is allocated to the instance variables and the instance variables are initialized. In the code, the program can initialize the instance variables in three places:

L specify the initial value when defining instance variables;

L specify the initial value for instance variables in non-static initialization blocks;

L initialization specified for instance variables in the constructor.

The first two methods are executed earlier than the last method, but the execution sequence of the first two methods is the same as that of the source program. For example:

 

Class nofive {name = "Xiao Wu Ling";} string name = "Ling Xiao Wu"; Public void output () {system. Out. println (name );}}

 

The name value output by the output method of the above program should be "Ling xiaowu ".

3) initialization time of class variables

In a program, JVM only initializes a Java class once. Therefore, every time a Java program runs, the system only allocates memory space for the class variable and executes initialization once. In the code, the program can initialize class variables in two places:

L specify the initial value when defining class variables;

L specify the initial value for the class variable in the static initialization block.

The execution sequence of these two methods is the same as that in the source program. The following is an example:

 

Public class test01 {// specify the initial value static string name = "" during definition; // use the static initialization block to specify the initial value static {system for the ID class variable. out. println ("static initialization block"); Id = 6190014;} static int id = 41009160; public static void main (string [] ARGs) {system. out. println ("name variable value:" + test01.name); system. out. println ("ID variable value:" + test01.id );}}

 

The output results of the above program are "Xiao Wu" and "41009160 ". The program execution sequence is: the system first allocates memory space for all class variables, and then executes the initial values specified in the static initialization block and the initial values specified when class variables are defined in the order listed in the source code.

The following uses a typical example to better understand the initialization process of class variables. The price class is defined first. There is a static initprice variable in the price class to represent the initial price. Each time a price instance is created, the system will take initprice as the basis, minus the current discount price (represented by the discount parameter) to get the currentprice variable value of the price.

 

Class price {// The class member is the final static price instance of the price instance = new price (2.8); // define another class variable static double initprice = 20; // define the currentprice instance variable double currentprice for this price; Public price (double discount) {// calculate the instance variable currentprice = initprice-discount according to the static variable ;}} public class test01 {public static void main (string [] ARGs) {// access the currentprice instance variable system through the price instance. out. println (price. instance. currentprice); // display the created price instance price P = new price (2.8); // access the currentprice instance variable system by displaying the created price instance. out. println (P. currentprice );}}

 

On the surface, the program outputs both the currentprice of two prices and should return 17.2 (20-2.8), but the output result is-2.8 and 17.2 after running.

If you only look at this problem on the Code surface, it is difficult to get the correct results. The following will analyze this program from the memory perspective.

First, check the main function in the pricetest class and execute system. out. println (price. instance. currentprice); when the program will use the price class for the first time, the price class will be initialized at this time, the initialization process is:

A. allocate memory space for the two class variables (instance and initprice) of price. The default values of instance and initprice are null and 0.0;

B. initialize the class variables in the order of the initialization code (specify the initial values and the initial values in the initialization block when defining:

A) initialize the instance: Create a price instance and use the price class parameter constructor. Execute currentprice = initprice-discount. Because initprice = 0.0 at this time, currentprice =-2.8;

B) initialize initprice: initprice = 20;

C. At this time, system. Out. println (price. instance. currentprice) in main () is executed, and the result of publishing is-2.8.

D. Then run price P = new price (2.8). In this line of code, the ABC process is executed first. At this time, currentprice =-2.8, initprice = 20. Call the price constructor to execute currentprice = initprice-discount and get currentprice = 17.2.

 

 

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.