Talking about JVM and memory allocation, talking about JVM Memory Allocation

Source: Internet
Author: User

Talking about JVM and memory allocation, talking about JVM Memory Allocation

1. program memory allocation

Initial Memory Allocation

When a program is ready to run, it first needs memory from the Java virtual machine, but the Java Virtual Machine itself does not have the permission. It can only apply for memory from the operating system. At this time, the Java Virtual Machine will have an initial memory,

Here are several additional parameters in the eclipse. ini file:

-Vmargs-Xms256m-Xmx512m-XX: PermSize = 256 M-XX: MaxPermSize = 512 M
-Vmargs indicates that VM parameters are followed, so the following are actually JVM parameters.
-Xms256m heap memory initially allocated by JVM
-Xmx512m maximum heap memory that can be allocated by JVM, which can be allocated as needed
-XX: PermSize = 256 M non-heap memory initially allocated by JVM
-XX: MaxPermSize = 512 M maximum non-heap memory that can be allocated by JVM, which is allocated on demand

Then, the Java Virtual Machine is opening memory for the program. When the program requires more space, what should I do if the initial memory is occupied? Of course, it will continue to apply to the operating system, for example:

 

So the question is, it cannot be applied all the time? Right? When it is always used up, when the maximum heap memory size is exceeded, it will not be distributed and will notify the memory overflow!

2. Who is occupying the memory?

I believe this figure shows that there are seven memory areas in the jvm. Now we only discuss three of them, stack blocks, heap blocks, and method areas:

Fast stack storage: Object Reference, basic data type;

Storage in heap blocks: specific objects;

Method Area: complete class structure.

Iii. analyze an instance:

Person class

public class Person {    public String name = null;    public int age = -1;        public Person() {        // TODO Auto-generated constructor stub    }    public Person(String name, int age) {        this.name = name;        this.age = age;    }        public void showMe() {        System.out.println(name + "/" + age);    }}

Main Method

Public class PersonRun {public static void main (String [] args) {Person pj = null; pj = new Person ("Putin", 45); pj. showMe (); Person obm = null; obm = new Person ("Obama", 50); obm. showMe (); int num = 10; System. out. println (num); int [] n = new int [] {1, 2, 3 };}}

Memory Distribution chart:

Note:

When we compile a person class, when the program is running, the class structure will be allocated to the method area, and it will only have one copy;

When Person pj = null; it is placed in the stack area, new Person ("Putin", 45); it is placed in the stack, for example, the address is 1000d, it will not open space for the method, but will be found in the method when we use it;

Then we use a reference to point to it for convenience. For example, pj's stack address: 100z, pj stores a 1000d address. When we call the showMe () method, find 100z first, find 1000d, and find the method in the method area.

The basic data type, such as int num = 10, is obtained directly from the stack when the program uses it. For array type n, it has reference similar to pj.

 

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.