新生代、老年代、永久代,具体不说了。
JVM 还有个 Thread Local Allocation Buffer (TLAB)的概念。JVM 为每个线程分配一个私有的缓存区域,多个线程分配内存时,为避免操作同一个地址,会需要使用加锁机制,进而影响分配速度。TLAB 分配仍然在堆上,是分配在Eden 区域内的。
start 、 end 是起止地址, top 表示已经分配到哪 , JVM 会移动到 top, 当 top 和 end 相遇的时候,表示缓存满了。
堆外内存就是把内存对象分配在Java虚拟机的堆以外的内存
DirectByteBuffer(int cap) { // package-private 私有包
super(-1, 0, cap, cap);
// 是否为页对齐的内存
boolean pa = VM.isDirectMemoryPageAligned();
// 获取页尺寸
int ps = Bits.pageSize();
long size = Math.max(1L, (long)cap + (pa ? ps : 0));
// 保留内存
Bits.reserveMemory(size, cap);
long base = 0;
try {
// 分配内存
base = unsafe.allocateMemory(size);
} catch (OutOfMemoryError x) {
// 如果内存溢出就去除保留内存
Bits.unreserveMemory(size, cap);
throw x;
}
// 设置内存
unsafe.setMemory(base, size, (byte) 0);
if (pa && (base % ps != 0)) {
// Round up to page boundary 向上取整至页面边缘
address = base + ps - (base & (ps - 1));
} else {
address = base;
}
cleaner = Cleaner.create(this, new Deallocator(base, size, cap));
att = null;
}
JConssole 查看 内存,可以看到 Metaspace 这个就是堆外内存