一. 前言
堆内存:jvm内存中最重要的使用最多的一块内存。
直接内存:jvm以外的,机器内存。
jdk1.8开始,舍弃原有的方法区,引入新的元空间。元空间存储的内容并无大的改变。元空间从jvm的内存模型中移出,使用直接内存。那为什么元空间使用直接内存?如果直接内存具有较好的优势,那为什么还要保留jvm内存?
二. 实例
import java.nio.ByteBuffer;
public class HeapAndDirectMemoryTest {
public static void main(String[] args) throws InterruptedException {
HeapAndDirectMemoryTest dm = new HeapAndDirectMemoryTest();
dm.heap();
dm.direct();
System.out.println("------------end------------");
}
private void heap() {
// 单次分配堆内存
long start = System.currentTimeMillis();
ByteBuffer bb = ByteBuffer.allocate(1024*1024*10*4);
System.out.println("单次分配堆内存:" + (System.currentTimeMillis() - start));
// 写堆内存
start = System.currentTimeMillis();
for(int i = 0 ; i < 1024*1024*10; i++) {
bb.putInt(i);
}
System.out.println("写堆内存: " + (System.currentTimeMillis() - start));
// 读堆内存
start = System.currentTimeMillis();
for(int i = 0; i < bb.array().length; i++) {
if (i == bb.array().length) {
System.out.println("--" + bb.array()[i]);
}
}
System.out.println("读堆内存:" + (System.currentTimeMillis() - start));
// 多次分配堆内存
start = System.currentTimeMillis();
for(int i = 0; i < 10000000; i++) {
ByteBuffer.allocate(1024);
}
System.out.println("多次分配堆内存:" + (System.currentTimeMillis() - start));
}
private void direct() {
// 单次分配直接内存
long start = System.currentTimeMillis();
ByteBuffer bb = ByteBuffer.allocateDirect(1024*1024*10*4);
System.out.println("单次分配直接内存:" + (System.currentTimeMillis() - start));
// 写直接内存
start = System.currentTimeMillis();
for(int i = 0 ; i < 1024*1024*10; i++) {
bb.putInt(i);
}
System.out.println("写直接内存: " + (System.currentTimeMillis() - start));
// 读直接内存
start = System.currentTimeMillis();
for(int i = 0; i < 1024*1024*10; i++) {
if (i == 1024*1024*10) {
System.out.println("--" + bb.get(i));
}
}
System.out.println("读直接内存:" + (System.currentTimeMillis() - start));
// 多次分配直接内存
start = System.currentTimeMillis();
for(int i = 0; i < 10000000; i++) {
ByteBuffer.allocateDirect(1024);
}
System.out.println("多次分配直接内存:" + (System.currentTimeMillis() - start));
}
}
三. 运行结果
单次分配(ms) | 写内存(ms) | 读内存(ms) | 多次分配(10000000次)(ms) | 多次分配(100000000次)(ms) | |
---|---|---|---|---|---|
堆内存 | 15 | 25 | 13 | 1630 | 9125 |
直接内存 | 5 | 17 | 5 | 8132 | 97310 |
四. 分析
运行结果表明,直接内存在单次分配、写内存、读内存都要明显优于堆内存,但是在我们频繁分配内存时,堆内存明显优于直接内存。而且随着分配频率的增加堆内存的优势越明显。元空间主要存储加载的类信息,这些数据只会在程序启动时分配内存,运行期一般不会频繁加载新的类,故运行期不需要频繁分配内存。元空间在程序启动时直接分配足够的直接内存,可以减少程序的启动时间,同时不会对运行时产生影响。堆内存主要存放的是运行时对象,需要频率的创建与销毁。