Java area of memory and memory overflow exception, to create an object

A runtime data area

    

  Java program execution flow: firstly .java source code files are Java bytecode compiler as files (.class suffix), and then loads each class bytecode files by the JVM class loader, after loading is completed, referred JVM execution engine (including in-time compiler, garbage collector) execution. In the course of the entire program execution, JVM will need to use the data and information during the execution period of space to store the program, this space called the run-time data area.

  1. Program Counter: is a small memory space, it can be considered as the current line number indicator thread bytecode executed, each thread has a separate program counter, so it is a private thread; this memory area is the only area not provide any OutOfMemoryError (OOM) situation in the Java virtual machine specification.

  2.Java virtual machine stack: is the thread private, its life cycle and the same thread. Virtual machine stack Java memory model described method performed: In each method are performed simultaneously to create a stack frame for storing local variable table (stored for all the basic data types compile knowable, and returnAddress object reference type) , operand stack, dynamic lists, and other information for export. From each method call to complete execution of the process corresponds to a stack frame in a virtual machine stack from the stack to the stack.

    Exceptions: If the stack is greater than the depth of the thread requested virtual machine allowable depth, StackOverflowError will throw an exception.

         You can not apply enough memory to a virtual machine if the dynamic expansion will be thrown OOM exception.

    Solution: For SOF appear abnormal, the stack can find the problem by reading error; for the OOM exception, without reducing the number of threads or replace the high virtual machine, only in exchange for more by reducing the maximum heap and stack capacity reduction the rout.

  3. The native method stacks: the use of virtual machines to serve Native method, the thread is private. SOF and it throws OOM exception.

  4.Java heap: is shared by all threads in a data area. Created when the virtual machine starts; its purpose is to store the object instance, and allocate memory for the object instance; Java heap garbage collector is the main area management; therefore, Java heap can be subdivided into the new generation and the old era.

    Exceptions: If there is no complete examples in the heap memory allocation, and the stack can no longer expand, it will throw OOM exception.

    Solution: First analyzed by memory-mapped snapshot of the heap dump analysis tool Dump out, make it clear in the end is a memory leak or memory overflow; if it is a memory leak, you can find further object through the tool is leaking through what path GC Roots associate and lead to the garbage collector can not automatically recover them, have information leaked object type information and GC Roots chain of references, you can more accurately locate the position of the leaked code; if the leak does not exist, that is, memory the objects must be alive, it would heap parameters should check the virtual machine's physical memory and its comparison to see if you can still turn up, from the code that checks for the presence of certain object life cycle is too long, too long held by state the case, try to reduce memory consumption during the run.

  The method area: is a thread shared memory area for storing class information have been loaded in the virtual machine, a constant, static variables, the code data time compiler mutation. Java Virtual Machine Specification limitation on the method very relaxed area, in addition to and do not require continuous as the Java heap memory, and can select a fixed size or may be expanded, but also can choose not to implement garbage collection. This area is mainly for garbage collection and recycling of the type of constant pool of unloading.

  Runtime constant pool is part of the method area for storing various literal and compile generated reference symbols, this part is brought to run in the methods of classes loaded zone time constant storage pool.

    Exceptions: when the method of memory allocation area can not meet the needs, will throw OOM exception.

    Solution: Load the GC class of useless for recycling.

  直接内存不是虚拟机运行时数据区的一部分,但是这部分内存也被频繁使用。本机直接内存不受Java堆大小的限制,但会受到本地总内存大小及处理器寻址空间的限制。NIO类引入了一种基于通道和缓冲区的IO方法,它可以使用Native函数库直接分配堆外内存,然后通过一个存储在Java堆中的DirectByteBuffer对象作为这块内存的引用进行操作。

    异常情况:当各个内存区域总和大于物理内存限制,导致动态扩展时出现OOM异常。

二、对象的创建过程

  当虚拟机遇到一条new指令时,首先先检查这个指令的参数是否能在常量池中定位到一个类的符号引用,并且检查这个符号引用代表的类是否已被加载、解析和初始化过。如果没有,那必须先执行类的加载过程。

  在类加载检查通过后,虚拟机将为新生对象分配内存。对象所需的内存大小在类加载完成时就已经确定;选择哪种分配方式由Java堆是否规整决定如果Java堆中的内存是规整的,所有用过的内存放在一边,空闲内存放在另一边,中间放着一个指针作为临界指示器,那分配内存就是把指针向空闲空间那边挪动一段与对象大小相等的距离,这种分配方法称为指针碰撞如果Java堆中内存是不规整的,虚拟机就必须维护一个列表,用来记录哪块内存时可用的,在分配时从列表中找到一块足够大的空间划分给对象实例,并更新列表上的记录,这种分配方法称为空闲列表

  另外在多线程情况下,对象创建并不是线程安全的,可能出现正给对象A分配内存,指针还没来得及修改,对象B又同时使用了原来的指针来分配内存的情况。解决这种问题的方案,一种是对分配内存空间的动作进行同步处理--实际上虚拟机采用CAS配上失败重试的方式保证更新操作的原子性;另一种是把内存分配的动作按照线程划分在不同的空间进行,即每个线程在Java堆中预先分配一小块内存,称为本地线程分配缓冲。哪个线程要分配内存,就在哪个线程的TLAB上分配,只有TLAB用完并分配新的TLAB时,才需要同步锁定。

  内存分配完成后,虚拟机需要将分配到内存空间都初始化为零值。这一操作保证了对象的实例字段在Java代码中可以不赋初始值就直接使用。接下来,虚拟机对对象进行必要的设置,并将这些设置存放在对象的对象头中。最后执行 init方法把对象按照意愿进行初始化,这样一个真正可用的对象就产生了。

三、对象的布局

  对象在内存中存储的布局可以分为:对象头、实例数据和对齐填充。对象头包括两部分信息:一部分用于存储对象自身的运行时数据;另一部分是类型指针,即对象指向它的类元数据的指针,虚拟机通过这个指针确定这个对象是哪个类的实例。实例数据部分是对象真正存储的有效信息。对齐填充并不是必然存在的,它仅仅起到占位符的作用。

四、对象的访问定位

  如果使用句柄访问的话,那么Java堆将会划分一块内存来作为句柄池,referance中存储的是对象的句柄地址,而句柄中包含了对象实例数据与类型数据各自的具体地址信息。

    使用句柄访问的好处就是referance中存储的是稳定的句柄地址,在对象被移动时只会改变句柄的实例数据指针,而referance本身不需要修改。

  如果使用直接指针访问,那么Java堆对象的布局就必须考虑如何放置访问类型数据的相关信息,而referance中存储的直接是对象的地址。

    使用直接指针访问的好处就是速度快,它节省了一次指针定位的开销。

Guess you like

Origin www.cnblogs.com/HuiH/p/11972170.html