堆栈内存,顾名思义,指的是堆内存以及栈内存,其中,堆内存是由Java GC进行管理的内存区域,而栈内存则是线程内存。关于栈内存,这里不去细说。以Hotspot为例,堆内存的简要结构如下图所示:
而堆栈的关系,我们可以通过一行简单的代码来理解:
public static void main(String[] args) {
Object o = new Object();
}
上述代码主要完成了两件事,new Object( ) 在堆上开辟了一块内存,也就是说,new Object( )是分配在堆上的;而变量o,则是在线程main的栈上面的,它指向了new Object( ) 开辟的堆内存地址。简单来说,程序中创建的对象,都存储在堆内存中,栈内存包含对它的引用。
简单来说,除了堆栈内存,剩下的就都是堆外内存了(当然,这是从Java运行时内存的角度来看),堆外内存直接受操作系统管理,而不是虚拟机。而使用堆外内存的原因,主要有几点:
总结上述内容中对堆栈内存与堆外内存的说明,主要解决了两个疑问:“零拷贝”是从哪拷贝到哪?“零拷贝”是怎么优化掉这一拷贝操作的?
在Java中,提供了一些使用堆外内存以及DMA的方法,能够在很大程度上优化用户进程的IO效率。这里,给出一份拷贝文件的代码,分别使用BIO、NIO和使用堆外内存的NIO进行文件复制,简单对比其耗时。
这里我使用一个200MB左右的pdf文件进行拷贝操作,你可以另外指定更大的文件,文件越大对比越明显。这里我运行出来的延时,BIO的平均耗时1500ms上下,NIO耗时120ms左右, 使用堆外内存的NIO耗时100ms上下。
package top.jiangnanmax.nio;
import java.io.*;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
/**
* @author jiangnanmax
* @email jiangnanmax@gmail.com
* @description CopyCompare
* @date 2021/5/7
**/
public class CopyCompare {
public static void main(String[] args) throws Exception {
String inputFile = "/tmp/nio/input/HyperLedger.pdf";
String outputFile = "/tmp/nio/output/HyperLedger.pdf";
long start = System.currentTimeMillis();
nioCopyByDirectMem(inputFile, outputFile);
long end = System.currentTimeMillis();
System.out.println("cost time: " + (end - start) + " ms");
deleteFile(outputFile);
}
/**
* 使用传统IO进行文件复制
*
* 平均耗时 15** ms
*
* @param sourcePath
* @param destPath
*/
private static void bioCopy(String sourcePath, String destPath) throws Exception {
File sourceFile = new File(sourcePath);
File destFile = new File(destPath);
if (!destFile.exists()) {
destFile.createNewFile();
}
FileInputStream inputStream = new FileInputStream(sourceFile);
FileOutputStream outputStream = new FileOutputStream(destFile);
byte[] buffer = new byte[512];
int lenRead;
while ((lenRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, lenRead);
}
inputStream.close();
outputStream.close();
}
/**
* 使用NIO进行文件复制,但不使用堆外内存
*
* 平均耗时 1** ms, 比BIO直接快了一个数量级???
*
* @param sourcePath
* @param destPath
*/
private static void nioCopy(String sourcePath, String destPath) throws Exception {
File sourceFile = new File(sourcePath);
File destFile = new File(destPath);
if (!destFile.exists()) {
destFile.createNewFile();
}
FileInputStream inputStream = new FileInputStream(sourceFile);
FileOutputStream outputStream = new FileOutputStream(destFile);
FileChannel inputChannel = inputStream.getChannel();
FileChannel outputChannel = outputStream.getChannel();
// transferFrom底层调用的应该是sendfile
// 直接在两个文件描述符之间进行了数据传输
// DMA
outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
inputChannel.close();
outputChannel.close();
inputStream.close();
outputStream.close();
}
/**
* 使用NIO进行文件复制,并使用堆外内存
*
* 平均耗时100ms上下,比没使用堆外内存的NIO快一点
*
* @param sourcePath
* @param destPath
*/
private static void nioCopyByDirectMem(String sourcePath, String destPath) throws Exception {
File sourceFile = new File(sourcePath);
File destFile = new File(destPath);
if (!destFile.exists()) {
destFile.createNewFile();
}
FileInputStream inputStream = new FileInputStream(sourceFile);
FileOutputStream outputStream = new FileOutputStream(destFile);
FileChannel inputChannel = inputStream.getChannel();
FileChannel outputChannel = outputStream.getChannel();
MappedByteBuffer buffer = inputChannel.map(FileChannel.MapMode.READ_ONLY, 0, inputChannel.size());
outputChannel.write(buffer);
inputChannel.close();
outputChannel.close();
inputStream.close();
outputStream.close();
}
/**
* 删除目标文件
*
* @param target
*/
private static void deleteFile(String target) {
File file = new File(target);
file.delete();
}
}
到此这篇关于详细总结Java堆栈内存、堆外内存、零拷贝浅析与代码实现的文章就介绍到这了,更多相关Java堆栈内存 堆外内存 零拷贝浅析内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
免责声明:本站发布的内容(图片、视频和文字)以原创、来自本网站内容采集于网络互联网转载等其它媒体和分享为主,内容观点不代表本网站立场,如侵犯了原作者的版权,请告知一经查实,将立刻删除涉嫌侵权内容,联系我们QQ:712375056,同时欢迎投稿传递力量。
Copyright © 2009-2022 56dr.com. All Rights Reserved. 特网科技 特网云 版权所有 特网科技 粤ICP备16109289号
域名注册服务机构:阿里云计算有限公司(万网) 域名服务机构:烟台帝思普网络科技有限公司(DNSPod) CDN服务:阿里云计算有限公司 百度云 中国互联网举报中心 增值电信业务经营许可证B2
建议您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流浏览器浏览本网站