Java memory structure, initialization of classes, and object construction process

Source: Internet
Author: User

    1. Overview

There are a lot of articles on this topic on the Internet, and I think it might be better to relate them to each other. Like other languages, it allocates memory space before executing the program we write, so that it can store code and data, and the execution of the program is still the execution of the code and the process of reading and writing the data; In addition to executing the explicit visible code we write, the JVM will automatically help us with some things, including loading, initializing, GC and so on, these are not special. The following are the concepts of each.

    1. Java Memory Architecture

This is basically the same as in other high-level languages, Java includes: heap , stack , local stack (some special), Permanent code area . The following details the function of each memory area and how to use it

concept

Modify method

Span style= "color:black; font-family: Microsoft ya black; FONT-SIZE:10PT "> Permanent code area
Permanent space

In fact, the code two words I added, it is not just store code, But the code is really the main, most typical, for a class A, no matter how many objects are instantiated, its code area has only one piece of the beginning. This area contains information about the class, such as the static field, the final constant (with the compiler or some relationship), various methods (including static and non-static decorated) binary heavenly book, etc., which tend to be the same size throughout the JVM execution.

xx:permspace
and
XX: Maxpermspace

Span style= "color:black; font-family: Microsoft ya black; FONT-SIZE:10PT "> local stack
native stack

for Java native threads, This is the stack used by the thread added in JNI, because I have not written much JNI code, this piece is unknown.

Span style= "color:black; font-family: Microsoft ya black; Font-size:10pt "> stack
Stack

when it comes to the stack, the thread contains the main thread we normally use. , and we call Thread.Start () run up the general thread, a thread a stack, the role of the stack should be clear, is used to keep our thread execution of the scene, including: The caller function of the local variables, parameters, etc., the stack is characterized by the storage of things are very small, access speed quickly.

XSS
and
-xx: Mainthreadstacksize

Heap
Heap

Heap, should be memory is occupied the largest part, and according to the JVM configuration, the heap can account for more than 95% of the JVM's total use of memory, of course, this number does not make much sense, just give us a feeling, compared to other languages, Java heap better understanding, all our new objects are stored in the heap, And its users invoke it through "references," which are often pushed into the stack we mentioned before (the reference is very small, so it's good for the stack to go in and out of it).

Xms Initial Size
And
XMX Maximum Value

And

XMN Young Generation Size


?

* Note : + The sum of the machine memory settings of a bit is generally not greater than 1.5G ( Even if you have 4G of physical memory, it may be related to the way of addressing Java, whether there are other solutions, but also look at the expert gives )

Let me give you an example here.

public class Demo {//Permanent Code area <-General 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 volume references (not visible to programmers) tend to be in registers, or other temporary places
String str = ""; Examples of STR in the heap
System.out.println ("I'm calling another method now");//The reference to I, STR is pressed into the stack, and the instance of STR is in the heap
}

public void Mtd () {///Permanent Code area <-code block (but with access restrictions, only objects can be referenced to that block of code)
}
}

Complete this part, the rest of the two parts would like to be simple:

    1. Class is loaded and initialized

classes are loaded and initialized before the first use, depending on the operating environment. The initialization of the class is to allocate memory space for classes and fields in the permanent code area , and then assign or execute static in the order in which we write. block.

Like what:

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

// The order of assignment and execution is the order of writing.

    1. Object construction Process

The parent class must be called before the subclass is constructed, and the building block will be executed before the constructor (I would like to know exactly how it was implemented, master Wang)

As an example:

A class:

public class A {

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

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

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

B class:

public class B extends A {
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 ();
}
}

Results:

A static block
B static block
A not static [email protected]
A Constructor
B not static block
B Constructor

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java memory structure, initialization of classes, and object construction process

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.