java 面试,jvm笔记

概念:JVM(Java Virtual Machine)是用来执行java字节码的环境,我们编写的java源代码通过javac命令编译之后,变成二进制的字节码指令,这些指令不是操作系统的指令,所以操作系统是识别不了这些字节码指令的。如果想要执行这些字节码指令,就需要先将其翻译或者说解释为操作系统自己的指令,这样我们写的代码才能被正确执行。 jvm就是完成这个解释执行的任务的,我们通过java命令执行我们编译好的代码时,会启动一个jvm的进程,jvm会将我们通过javac编译好的字节码指令,翻译成操作系统指令。由于不同的操作系统指令是不同的,这样就需要实现各个操作系统平台的jvm,由他们将相同的字节码指令翻译为各个操作自己的指令进行执行,这样通过不同操作系统的jvm,我就可以实现,同一份字节码指令可以在不同操作系统上执行。

一、JVM 的内存逻辑划分

1.概念

JVM是一个进程,所以操作系统就会给它分配一定量的系统资源,比如说,cpu资源,内存资源

2.JVM内存的划分

3.Java虚拟机规范中对各个区域的定义

方法区**(Method Area)**

The Java Virtual Machine has a method area that is shared among all Java Virtual Machine threads. The method area is analogous to the storage area for compiled code of a conventional language or analogous to the "text" segment in an operating system process.

It stores per-class structures such as the run-time constant pool, field and method data, and the code for methods and constructors, including the special methods (S2.9) used in class and instance initialization and interface initialization.

The method area is created on virtual machine start-up. Although the method area is logically part of the heap, a simple implementation may choose not to either garbage collect or compress it.

The Java Virtual Machine specification does not mandate the location or management of compiled code. The method area may be of fixed size or may be expanded as required by the computation and may be contracted if a larger method area is no longer required.

If the method area cannot be allocated enough memory to satisfy a request, the Java Virtual Machine will throw an OutOfMemoryError.

Java 虚拟机有一个方法区(method area),它被所有 Java 虚拟机线程共享。方法区类似于传统语言编译代码的存储区域,或者类似于操作系统进程中"文本段(text segment)"。它存储每个类的结构信息,例如运行时常量池、字段和方法的数据,以及方法和构造函数的代码,包括用于类和实例初始化以及接口初始化的特殊方法(见第 2.9 节)。

方法区在虚拟机启动时被创建。虽然方法区在逻辑上是堆的一部分,但简单的实现可以选择不对它进行垃圾回收或压缩。本规范不强制规定方法区的位置或管理编译代码的策略。方法区可以是固定大小的,也可以根据计算需要进行扩展,并在不再需要更大的方法区时进行收缩。方法区的内存不需要是连续的。

Java 虚拟机实现可以为程序员或用户提供对方法区初始大小的控制,如果方法区的大小是可变的,还可以提供对最大和最小方法区大小的控制。

与方法区相关的异常情况如下:

  • 如果方法区中无法分配足够的内存以满足请求,Java 虚拟机会抛出 OutOfMemoryError 异常。
堆(Heap)

The Java Virtual Machine has a heap that is shared among all Java Virtual Machine threads. The heap is the runtime data area from which memory for all class instances and arrays is allocated.

The heap is created on virtual machine start-up. Heap storage for objects is reclaimed by an automatic storage management system, which is known as a garbage collector. Garbage collection is a form of automatic memory management that reclaims memory that was allocated to objects that are no longer reachable from any live thread.

The Java Virtual Machine specification does not mandate a particular garbage collection method.

The heap may be of fixed size or may be expanded as required by the computation and may be contracted if a larger heap is no longer required.

If the Java Virtual Machine cannot allocate an object in the heap because no space is available, the Java Virtual Machine will throw an OutOfMemoryError.

虚拟机栈(Java Virtual Machine Stacks)

Each Java Virtual Machine thread has a private Java Virtual Machine stack, created at the same time as the thread. A Java Virtual Machine stack stores frames.

A Java Virtual Machine stack is analogous to the stack of a conventional language such as C: it holds method invocations and local variables.

If the computation of a Java Virtual Machine thread requires a larger Java Virtual Machine stack than is available, the Java Virtual Machine will throw a StackOverflowError.

If the Java Virtual Machine is unable to allocate space for an expansion of a Java Virtual Machine stack, the Java Virtual Machine will throw an OutOfMemoryError.

本地方法栈(Native Method Stacks)

The Java Virtual Machine may support native code. The Java Virtual Machine specification does not mandate whether native code is supported.

If the Java Virtual Machine supports native code, it must provide native method stacks for each thread. The native method stack is used to support native methods (written, for example, in C or C++).

The behavior of the native method stack is similar to that of the Java Virtual Machine stack.

If the computation of a native method requires a larger native method stack than is available, the Java Virtual Machine will throw a StackOverflowError.

If the Java Virtual Machine is unable to allocate space for an expansion of a native method stack, the Java Virtual Machine will throw an OutOfMemoryError.

程序计数器(Program Counter Register)

Each Java Virtual Machine thread has its own program counter register. At any given point, a Java Virtual Machine thread is executing exactly one method: either a Java Virtual Machine method (a Java method) or a native method.

If the current method is not native, the program counter register contains the address of the Java Virtual Machine instruction currently being executed. If the current method is native, the value of the program counter register is undefined.

The program counter register is the only part of the Java Virtual Machine runtime data area that is not subject to any OutOfMemoryError.

相关推荐
mldlds2 小时前
Spring Boot应用关闭分析
java·spring boot·后端
woniu_buhui_fei2 小时前
Java 服务最常见的线上性能故障
java·jvm·算法
96772 小时前
Java 类映射数据库表的核心规则
java·数据库·oracle
星辰_mya2 小时前
Redis 锁的“续命”艺术:看门狗机制与原子性陷阱
数据库·redis·分布式·缓存·面试
阳光下的米雪2 小时前
存储过程的使用以及介绍
java·服务器·数据库·pgsql
yoyo_zzm2 小时前
Spring Boot 各种事务操作实战(自动回滚、手动回滚、部分回滚)
java·数据库·spring boot
tuyanfei2 小时前
Spring 简介
java·后端·spring
遥遥晚风点点2 小时前
JAVA http请求报错:unable to find valid certification path to requested target
java·网络·网络协议·http
ZhengEnCi2 小时前
J0A-JPA持久化技术专栏链接目录
java·数据库