Java Foundation---JVM memory management and memory run mechanism learning summary

Source: Internet
Author: User

I stitched up a JVM memory graph from online search: as shown in:

Let's consider a few questions:

How does the 1.jVM work?

How is the 2.JVM runtime memory allocated?

3. In what area do we write Java code (classes, objects, methods, constants, variables, etc.) in the end?

VM Run-time data region:

1. Procedure counter (program Counter Register):

is a small amount of memory space that can be used as the line number indicator of the bytecode that is being executed by the current thread. In the virtual machine concept model (only the conceptual model, the various virtual machines may be implemented in some more efficient way), the bytecode interpreter works by changing the value of this counter to select the next need to execute the bytecode instruction, branch, loop, jump, exception processing, thread recovery and other basic functions need to rely on this Counter to complete.

Because the multithreading of a Java Virtual machine is implemented in a way that threads rotate and allocate processor execution time, at any given moment, a processor (a kernel for a multicore processor) executes only the instructions in one thread. Therefore, in order to recover the thread after switching to the correct execution location, each thread needs to have a separate program counter, the counters between the threads do not affect each other, isolated storage, we call this type of memory area is "thread-private" memory.

If the thread is executing a Java method, this counter records the address of the executing virtual machine bytecode instruction, or null (Undefined) If the Natvie method is being executed. This memory area is the only area in the Java Virtual Machine specification that does not stipulate any outofmemoryerror conditions.

2.java VM Stack (Java virtual machine Stacks):

It is stored in a local variable table (which holds the various scalar types that are known at compile time: boolean,byte,char,short,int,float,long,double), the object's reference (not the object itself, just the reference pointer), the method return address, and so on.

Two exceptions are specified in the virtual stack:

  1. If the depth of the thread request is greater than the depth allowed by the virtual machine, the Stackoverflowerror exception is thrown, which is the stack overflow exception. It is easy to throw this exception in the case of recursive invocation methods.
  2. If the VM stack can be dynamically extended, when the extension cannot request enough memory space, throw OutOfMemoryError exception, memory overflow.

3. Local methods Stack (native method stacks)

This area has relatively little responsibility for running memory in the JVM. Just execute the native method. Stackoverflowerror and OutOfMemoryError exceptions are thrown if there is insufficient memory in this area.

4.java Heap

This area is the largest chunk of the JVM, and the Java heap is shared by all threads and is the GC's primary recycle zone, which is created when the JVM is started. The only purpose of the Java heap is to hold object instances (all new objects) where instances of most objects are allocated in this area.

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.