The JVM Thing (a)--JVM memory introduction

Source: Internet
Author: User
Tags garbage collection object object prev

This section Xiao Wang focuses on the next Java memory, first ask:

How 1.JVM memory is allocated

2. Why divide the new generation and the old age, in practice which belong to what belongs to the old

3. Briefly describe how JSTAT-GC


How 1.JVM memory is allocated 1.1 Concept Introduction

Memory is divided into two parts of the stack

Stack: Thread Private

Heap: Thread sharing


Stack:

Program counter: The byte code line number indicator executed by the current thread controls the execution of code bytes

Java Virtual Machine stack: Describes the memory model of the Java method execution, from the call to completion of the corresponding stack frame in the virtual machine stack into the stack to the process.

stack frame : A data structure used to store local variables, operand stacks, dynamic links, method exits

Local method Stack: Service for the native method of the virtual machine stack

Heap:

Java heap: Saving Java object instances

Method Area (Non-heap, Permanent generation): constants, static variables, class information

Run a constant pool: part of the method area, the literal and symbolic references of the compilation period

1.2 following code examples to deepen awareness

public class MyThreadPrinter2 implements Runnable {

private String name;
Private Object prev;
Private Object self;

private static int num = 12;


Private MyThreadPrinter2 (String name, Object prev, object self) {
THIS.name = name;
This.prev = prev;
This.self = self;
}

@Override
public void Run () {
int count = 10;
System.out.println (count);
}

public static void Main (string[] args) throws Exception {
Object A = new object ();
Object c = new Object ();
MyThreadPrinter2 pa = new MyThreadPrinter2 ("A", C, a);
New Thread (PC). Start ();

}
}


The code produces the object and thread, executes the thread, and analyzes the JVM memory when the Main method executes

1.

Object A = new object ();
Object c = new Object ();

Two object objects are generated in the Java heap

The local variable table in the stack generates two reference types A and C refer to two object instances in the heap, respectively

2.

MyThreadPrinter2 pa = new MyThreadPrinter2 ("A", C, a);

The Java heap produces a MyThreadPrinter2 object object that contains the variable name prev Self

At the same time, static num is saved in the method area

The local variable table in the stack produces the PA and points to the MyThreadPrinter2 object

The literal of the Run method information is saved in the method area

3.

New Thread (PC). Start ();

Produces a thread object and calls the Run method

The stack frame is generated for the thread object in the stack

4.

public void Run () {
int count = 10;
System.out.println (count);
}

The local variable table in the Run method generates a count and assigns a value of 10


Finally, the operation of the method is done by stacking the stack frame with the instruction set and calculating it.



2. Why divide the new generation and the old age, in practice which belong to what belongs to the old

From the perspective of memory recycling Java heap is divided into the new generation, the old age, the permanent generation

Permanent generation: The contents of the method area are put here

Cenozoic: The first generated object or array

Old age: Objects that were first generated by large objects or that survived after n garbage collection

Each time the old age is recycled, the permanent generation is recycled.


3.jstat GC describes How memory is allocated we can use Jstat GC to view
You can see that the new generation has three areas of Eden Survivor objects first assigned to Eden, and Eden is full and will be collected and then survived into the Survivor old age permanent generation Perm







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.