Java initialization, garbage collection, and memory allocation

Source: Internet
Author: User

2.1 initialization sequence: [java] public class Parent {static Tipout TIP = new Tipout ("Parent class static member TIP initialization "); tipout tip = new Tipout ("Parent class member tip initialization"); public Parent () {System. out. println ("Parent class constructor call") ;}} class Sub extends Parent {static Tipout SUB_TIP = new Tipout ("subclass static member SUB_TIP initialization "); tipout subTip = new Tipout ("subTip initialization for subclass members"); public Sub () {System. out. println ("subclass constructor call") ;}} class Tipout {pu Blic Tipout (String s) {System. out. println (s) ;}} public class Parent {static Tipout TIP = new Tipout ("Parent class static member TIP initialization "); tipout tip = new Tipout ("Parent class member tip initialization"); public Parent () {System. out. println ("Parent class constructor call") ;}} class Sub extends Parent {static Tipout SUB_TIP = new Tipout ("subclass static member SUB_TIP initialization "); tipout subTip = new Tipout ("subTip initialization for subclass members"); public Sub () {System. out. println ("subclass constructor call") ;}} Class Tipout {public Tipout (String s) {System. out. println (s) ;}} execution result: parent class static member TIP initialize subclass static member SUB_TIP initialize parent class member tip initialize parent class constructor call subclass member subTip initialize subclass constructor call this can summarize the java initialization sequence: -> All static member initialization (parent class> subclass) --> parent class initialization (common member> constructor) ---> subclass initialization (common member> constructor )! The initialization sequence of static members is only related to the sequence in the class definition. When static data and static methods are called, all static members in this class will be initialized (non-static members will not be initialized), provided that they have never been initialized. Test code: [java] public class Parent {public static Tipout TIP = new Tipout ("Parent class static member TIP initialization"); static {System. out. println ("Parent static code block");} Tipout tip = new Tipout ("Parent class member tip initialization"); public Parent () {System. out. println ("parent class constructor call");} public static void outMsg () {System. out. println ("Parent class static function call") ;}} public class Parent {public static Tipout TIP = new Tipout ("Parent class static member TIP initialization"); static {System. out. println ("Parent static code block");} Tipout tip = new Tipout ("Parent class member tip initialization"); public Parent () {System. out. println ("parent class constructor call");} public static void outMsg () {System. out. println ("Parent class static function call") ;}} call in Main (): Parent. TIP. toString (); Result: 2.2 garbage collection java does not need to clear the object, and there is no destructor in C ++. java's garbage collection mechanism will automatically release useless variables. An object that can point to one or more referenced variables. When an object no longer has any reference variable pointing to it, this object can be recycled by the garbage collection mechanism. However, the object is not recycled immediately after it is discarded. JVM process has a high system overhead for space collection. If an application discards an object every time, it immediately recycles its space, which will inevitably make the entire system very inefficient.! You cannot manipulate garbage collection to make the object Value null, or call System. after gc () JVM accepts this message, it does not immediately perform garbage collection, but only weighted several garbage collection algorithms to make garbage collection easy or happen early, or there are many recycles.! The running time of the garbage collector is not fixed, and the actual running time of the cleanup is unpredictable. 2.3 memory allocation mechanism 2.3.1Java divides memory into two types: 1. stack memory, basic types of variables and Object Reference variables (access speed is faster than the heap, second only to registers) 2. heap memory. Heap memory is used to store objects and arrays created by new.! A class, like an array, is a reference type. A reference type means that a heap of memory can be pointed by multiple stacks of memory at the same time. Java mainly has four Memory Spaces: stack memory space: stores all object names (more accurately, stores the referenced heap memory space address). heap memory space: save the specific attributes of each object. Global data zone: stores static attributes. Global Code area: stores all method definitions. 2.3.2 The String buffer pool is a java buffer pool designed specifically for strings in the memory to save memory space. If the two strings are the same, use the String in the pool, and do not create new objects. Use the following code to test: String str1 = "abc"; String str2 = "abc"; System. out. println (str1 = str2); // true indicates that str1 and str2 point to the same object String str1 = new String ("abc "); string str2 = new String ("abc"); System. out. println (str1 = str2); // If "new" is used, two objects are created in the heap. 2.4 note the following: 1. try to use the basic data type instead of the object String str = "hello"; the above method will create" Hello "string, and the JVM character cache pool also caches this string. String str = new String ("hello"); in this case, in addition to creating a String, the String object referenced by str also contains an array of char [] at the bottom, the char [] array stores h, e, l, l, o 2 in sequence. minimize repeated variable calculation [java] v for (int I = 0; I <list. size (); I ++) for (int I = 0; I <list. size (); I ++) should be changed to [java] view plaincopyprint? For (int I = 0, len = list. size (); I <len; I ++) for (int I = 0, len = list. size (); I <len; I ++) and avoid using complex expressions in a loop. in a loop, loop conditions are calculated repeatedly. If complex expressions are not used, if the cyclic condition value remains unchanged, the program runs faster. 3. avoid declaring the Object [java] for (int I = 0; I <10000; ++ I) {Object obj = new Object ();} in the loop body ();} for (int I = 0; I <10000; ++ I) {Object obj = new Object ();} although the performance difference is not big, but such code will waste the stack memory space. As mentioned above, the referenced variables will be stored in the stack memory, which will generate a large number of object references in the memory. It should be written as the following: [java] Object obj; for (int I = 0; I <10000; ++ I) {obj = new Object ();} Object obj; for (int I = 0; I <10000; ++ I) {obj = new Object ();}

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.