简体   繁体   English

Java新关键字内部

[英]Java new Keyword internals

JVM is responsible for allocating heap memory for objects created using new keyword in Java based on size of the object. JVM负责根据对象的大小为使用Java中的new关键字创建的对象分配堆内存。 How does memory allocation work internally. 内存分配如何在内部工作。 Does JVM maintain a pointer to next big enough free block of memory and returns it or it delegates the responsibility of memory allocation to OS through system calls, like malloc in C internally calls brk() ? JVM是否维护一个指向下一个足够大的空闲内存块的指针并返回它,还是通过系统调用将内存分配的职责委托给OS,例如C内部的malloc内部调用brk()?

It is JVM dependent and it is an implementation detail. 它依赖于JVM,并且是实现细节。 But it is usually done via TLAB - thread local allocation buffer, faster then malloc , since it's a simple pointer bump. 但这通常是通过TLAB完成的-线程本地分配缓冲区,比malloc快,因为它是一个简单的指针缓冲。 And that is a very-simplified explanation. 这是一个非常简单的解释。

The canonical Java implementation uses a generational strategy for memory allocation. 规范的Java实现使用世代策略进行内存分配。

https://docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/geninfo/diagnos/memman.html#wp1089132 http://www.oracle.com/technetwork/java/javase/memorymanagement-whitepaper-150215.pdf https://docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/geninfo/diagnos/memman.html#wp1089132 http://www.oracle.com/technetwork/java/javase/memorymanagement-whitepaper-150215.pdf

Objects are allocated in "heap" memory, which is divided into two areas called "generations". 对象在“堆”内存中分配,该内存分为两个区域,称为“世代”。 There is a "young generation" area that's further divided into "Eden" (aka the "nursery") and two "survivor spaces". 有一个“年轻一代”区域,进一步划分为“伊甸园”(又称“苗圃”)和两个“幸存者空间”。 There is also an "old", or "tenured" generation area, comprising essentially all the heap not in the young generation. 还有一个“旧”或“使用年限”的生成区域,基本上包括所有不在年轻一代中的堆。

Objects normally are allocated in Eden first. 通常,通常先在伊甸园中分配对象。 "Large" objects too big for Eden go into the tenured generation. 对于伊甸园来说太大的“大型”物体进入了终身制。

When Eden gets full, all the "live" objects, ones that are still reachable via some reference in the program, get copied into the first survivor space, then Eden is erased and the pointer to free Eden space is set to the beginning of the space. 当伊甸园已满时,所有“活动”对象(仍可通过程序中的某些引用访问)被复制到第一个幸存者空间,然后擦除伊甸园并将指向可用伊甸园空间的指针设置为空间。

Object allocation is simply a matter of designating the current "top" of Eden as the address of the new object, and the pointer to free Eden memory is increased by the size of the object. 对象分配只是将当前Eden的“顶部”指定为新对象的地址,而指向空闲Eden内存的指针将增加对象的大小。 This takes something like 10 ns on modern computers. 在现代计算机上,这大约需要10 ns。 There is no equivalent to "free" because individual objects are never freed; 因为没有释放单个对象,所以没有“释放”的含义。 the entire section of heap is simply erased and the free area pointer reset. 堆的整个部分都将被简单擦除,并释放可用区域指针。 When the first survivor space is full, then the JVM copies all the live objects from it and Eden into the other survivor space. 当第一个幸存者空间已满时,JVM将所有活动对象从它和Eden复制到另一个幸存者空间。 Eden and the first survivor space are erased and their pointers reset. 伊甸园和第一个幸存者空间将被擦除,其指针将重置。 Now the other survivor space becomes "first", the old one "second", and the process repeats. 现在,另一个幸存者空间变为“第一个”,旧的幸存者空间为“第二个”,然后重复该过程。

This is very speedy because dead objects are not tracked or marked, and well-written programs tend to have many relatively small objects of which most don't live long. 这是非常快速的,因为不跟踪或标记死对象,并且编写良好的程序往往具有许多相对较小的对象,其中大多数寿命不长。

When objects do live a long time, they get "promoted" out of the young generation to the tenured generation, which is collected less frequently using slower algorithms. 当对象确实存在很长时间时,它们就会从年轻一代“提升”为终身一代,而使用较慢的算法则不那么频繁地收集它们。 Tenured objects tend to die less quickly than in the young generation, and when they do, their space just becomes a "hole" of free space somewhere in the middle. 永久性物体的死亡速度不如年轻一代快,而一旦死亡,它们的空间就会变成中间某个地方的自由空间的“空洞”。 Old-generation garbage collection typically involves moving live objects next to each other to consolidate blocks of free memory into larger blocks. 老式垃圾收集通常涉及将活动对象彼此相邻移动,以将可用内存块合并为更大的块。

This is just a scratch of the surface. 这只是表面的划痕。 There's more to study. 还有更多的东西要研究。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM