Java memory structure, class initialization, and object construction process, java Construction

Source: Internet
Author: User

Java memory structure, class initialization, and object construction process, java Construction

There are already many articles on this topic on the Internet. I think it may be better to understand these articles. Like other languages, it allocates memory space before executing the program we write to store code and data; the execution process of the program is actually code execution and Data Reading and Writing. In addition to executing the explicit and visible code we write, the Jvm will automatically help us with some things, including class loading, initialization, GC, and so on. These concepts are described as follows.

This is basically the same as other advanced languages. Java includes:Heap,Stack,Local Stack(Some Special ),Permanent code area. The role and usage of each memory zone are described as follows:

 

Concept

Modification Method

Permanent code area
Permanent space

In fact, the two words of code are added by me. It is not just to store code, but the code is indeed the most important and typical. For A class A, no matter how many objects are instantiated, its code zone has only one block from the beginning and end. This section mainly contains class information, such as static fields, final constants (which are still related to the compiler), and binary tianshu of various methods (including static and non-static modifiers, this area is usually not changed throughout the JVM execution process.

XX: PermSpace
And
XX: MaxPermSpace

Local Stack
Native stack

It is used for java native threads, that is, the stack used by threads added in jni, because I have not written much jni code, which is not detailed here.

?

Stack
Stack

The stack must be associated with the Thread. The Thread contains the main Thread that we usually use and the Thread that we call. A general thread running by start (), one thread and one stack. The role of the stack should be clear to everyone, that is, to keep the scene of our thread execution, including: the caller's functions include local variables and parameters. The stack features very small storage and fast access.

Xss
And
-XX: MainThreadStackSize

Heap
Heap

The heap should be the most occupied part of the memory. Unlike the jvm configuration, the heap can account for more than 95% of the total memory used by the jvm. Of course, this number does not make much sense, it just gives you a feeling that, compared with other languages, the Java heap is better understood, and all the new objects are stored in the heap, the user calls it through "Reference", and the introduction is often pushed into the stack we mentioned earlier (the reference is very small, so it is suitable for inbound and outbound in the stack ).

Initial Xms size
And
Maximum Xmx Value

And

Xmn young generation size


 

*Note:: The total memory size of 32-bit machines cannot exceed 1.5 GB. (even if you have 4 GB physical memory, it may be related to the java addressing method. Is there any other solution, also hope that the high people will give)

Here is an example.

Public class Demo {// permanent code area <-class overall information
Public static String staticField; // permanent code area
Public String dynField; // heap

Public static void staticMtd () {// permanent code area <-code block
Int I = 0; // direct reference (invisible to programmers) is often in registers or other temporary places
String str = ""; // The str instance is in the heap.
System. out. println ("I'm calling another method now"); // at this time, I and str references are pushed to the stack, and str instances are in the stack.
}

Public void mtd () {// permanent code area <-code block (with access restrictions added, only objects can be referenced in this code block)
}
}

After completing this part, the remaining two parts are simple:

Class is loaded and initialized before it is used for the first time. The specific loading depends on the running environment. Class initialization is inPermanent code areaAllocate memory space for classes and fields, and then follow our writing orderSequentialAssign values or execute static blocks.

For example:

Static {System. out. println ("first exc ");}
Static String Field = "x ";
Static String F2 = Field;
Static {System. out. println ("after F2 = Field ");}

// The assignment and execution sequence are the writing sequence.

The parent class must be called before the subclass is constructed, and the construction block will be executed before the function is constructed (I want to know how to implement it, hope the master points out)

For example:

Class:

Public class {

Static {
System. out. println ("A static block ");
}

Public (){
Super ();
System. out. println ("A constructor ");
}

{
System. out. println ("A not static block" + this );
}
}

Class B:

Public class B extends {
Static {
System. out. println ("B static block ");
}

Public B (){
Super ();
System. out. println ("B constructor ");
}

{
System. out. println ("B not static block ");
}

Public static void main (String [] args ){
New B ();
}
}

Result:

A static block
B static block
A not static blockB @ a90653
A constructor
B not static block
B constructor

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.