Java中三種零拷貝的實(shí)現(xiàn)示例以及對(duì)比詳解
簡(jiǎn)介
本文主要是介紹幾種零拷貝的實(shí)現(xiàn)示例,以及與最傳統(tǒng)的做一個(gè)對(duì)比,看看在效率上到底有多大的提升
好了,廢話(huà)不多說(shuō)直接干,本章例子是通過(guò)網(wǎng)絡(luò)IO傳輸一個(gè)8M大小的文件,對(duì)比傳輸效率,由于服務(wù)端接收端不需要修改,所以我們先上服務(wù)端代碼:
public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(8080); System.out.println("服務(wù)端:等待連接"); Socket accept = serverSocket.accept(); System.out.println("服務(wù)端:" + accept.getRemoteSocketAddress() + "已連接"); File file = new File("C:\\Users\\Administrator\\Desktop\\ioTest.txt"); if(!file.exists()){ file.createNewFile(); } FileOutputStream fileOutputStream = new FileOutputStream(file); InputStream bufferedInputStream = accept.getInputStream(); byte[] bytes = new byte[2048]; int read; while ((read = bufferedInputStream.read(bytes,0,2048)) != -1) { fileOutputStream.write(bytes); } OutputStream outputStream = accept.getOutputStream(); outputStream.write("接收完畢".getBytes()); accept.shutdownOutput(); fileOutputStream.close(); outputStream.close(); bufferedInputStream.close(); accept.close(); }
傳統(tǒng)實(shí)現(xiàn)
正常的socket傳輸,耗時(shí):46ms
public static void normal() throws IOException { Socket socket = new Socket("127.0.0.1", 8080); OutputStream outputStream = socket.getOutputStream(); InputStream inputStream = socket.getInputStream(); long start = System.currentTimeMillis(); File file = new File("C:\\Users\\Administrator\\Desktop\\222.txt"); FileInputStream fileInputStream = new FileInputStream(file); byte[] bytes1 = new byte[2048]; while (fileInputStream.read(bytes1, 0, 2048) != -1) { outputStream.write(bytes1); } socket.shutdownOutput(); System.out.println("耗時(shí):" + (System.currentTimeMillis() - start)); byte[] bytes = new byte[1024]; String message = ""; int read; while ((read = inputStream.read(bytes)) != -1) { message += new String(bytes, 0, read); } System.out.println("服務(wù)端發(fā)來(lái)消息->" + message); inputStream.close(); outputStream.close(); socket.close(); }
MMAP
MMAP原理就是建立了一個(gè)文件映射,劃分了一個(gè)虛擬空間,往這個(gè)空間寫(xiě)數(shù)據(jù),少了一次拷貝
缺點(diǎn):空間有限
實(shí)踐案例:RocketMq
耗時(shí):32ms
public static void mmp() throws IOException { SocketChannel socketChannel = SocketChannel.open(); socketChannel.connect(new InetSocketAddress("127.0.0.1", 8080)); long start = System.currentTimeMillis(); Path path = Paths.get("C:\\Users\\Administrator\\Desktop\\222.txt"); FileChannel open = FileChannel.open(path, StandardOpenOption.READ); MappedByteBuffer map = open.map(FileChannel.MapMode.READ_ONLY, 0, open.size()); socketChannel.write(map); socketChannel.shutdownOutput(); System.out.println("耗時(shí):" + (System.currentTimeMillis() - start)); ByteBuffer allocate = ByteBuffer.allocate(1024); int read = socketChannel.read(allocate); if (read > 0) { allocate.flip(); byte[] bytes = new byte[allocate.remaining()]; allocate.get(bytes); System.out.println("服務(wù)端發(fā)來(lái)消息:" + new String(bytes)); } socketChannel.close(); }
transferTo
原理就是兩個(gè)通道之間直接傳輸數(shù)據(jù),根據(jù)系統(tǒng)支持程度,少了1-2次拷貝
缺點(diǎn):局限于文件通道
實(shí)踐案例:Netty、Kafka
耗時(shí):18ms
public static void transferTo() throws IOException { SocketChannel socketChannel = SocketChannel.open(); socketChannel.connect(new InetSocketAddress("127.0.0.1", 8080)); long start = System.currentTimeMillis(); Path path = Paths.get("C:\\Users\\Administrator\\Desktop\\222.txt"); FileChannel open = FileChannel.open(path, StandardOpenOption.READ); long l = open.transferTo(0, open.size(), socketChannel); socketChannel.shutdownOutput(); System.out.println("耗時(shí):" + (System.currentTimeMillis() - start)); ByteBuffer allocate = ByteBuffer.allocate(1024); int read = socketChannel.read(allocate); if (read > 0) { allocate.flip(); byte[] bytes = new byte[allocate.remaining()]; allocate.get(bytes); System.out.println("服務(wù)端發(fā)來(lái)消息:" + new String(bytes)); } socketChannel.close(); }
堆外內(nèi)存
原理直接使用堆外內(nèi)存,少了一次拷貝
缺點(diǎn):堆外內(nèi)存開(kāi)啟耗時(shí),此內(nèi)存不受JVM控制,如垃圾回收等
實(shí)踐案例:Netty
耗時(shí):26ms
public static void outSide() throws IOException { SocketChannel socketChannel = SocketChannel.open(); socketChannel.connect(new InetSocketAddress("127.0.0.1", 8080)); long start = System.currentTimeMillis(); Path path = Paths.get("C:\\Users\\Administrator\\Desktop\\222.txt"); FileChannel open = FileChannel.open(path, StandardOpenOption.READ); ByteBuffer byteBuffer = ByteBuffer.allocateDirect((int) open.size()); open.read(byteBuffer); byteBuffer.flip(); socketChannel.write(byteBuffer); socketChannel.shutdownOutput(); System.out.println("耗時(shí):" + (System.currentTimeMillis() - start)); ByteBuffer allocate = ByteBuffer.allocate(1024); int read = socketChannel.read(allocate); if (read > 0) { allocate.flip(); byte[] bytes = new byte[allocate.remaining()]; allocate.get(bytes); System.out.println("服務(wù)端發(fā)來(lái)消息:" + new String(bytes)); } socketChannel.close(); }
總結(jié)
耗時(shí)統(tǒng)計(jì)不完全準(zhǔn)確,都是多次取平均,具體使用哪種需要看場(chǎng)景來(lái)
到此這篇關(guān)于Java中三種零拷貝的實(shí)現(xiàn)示例以及對(duì)比詳解的文章就介紹到這了,更多相關(guān)Java零拷貝方式對(duì)比內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring boot + mybatis + orcale實(shí)現(xiàn)步驟實(shí)例代碼講解
這篇文章主要介紹了Spring boot + mybatis + orcale的實(shí)現(xiàn)步驟實(shí)例代碼講解,需要的朋友可以參考下2017-12-12使用Java實(shí)現(xiàn)2048小游戲代碼實(shí)例
這篇文章主要介紹了使用Java實(shí)現(xiàn)2048小游戲代碼實(shí)例,2048 游戲是一款益智類(lèi)游戲,玩家需要通過(guò)合并相同數(shù)字的方塊,不斷合成更大的數(shù)字,最終達(dá)到2048,游戲規(guī)則簡(jiǎn)單,但挑戰(zhàn)性很高,需要玩家靈活運(yùn)用策略和計(jì)算能力,本文將使用Java代碼實(shí)現(xiàn),需要的朋友可以參考下2023-10-10java 壓縮和解壓縮Zip、Jar、Gzip文件實(shí)例代碼
本文主要介紹java壓縮和解壓縮Zip、Jar、Gzip文件的知識(shí),這里整理了相關(guān)資料,并附示例代碼有興趣的小伙伴可以參考下2016-09-09Java使用PDFBox實(shí)現(xiàn)操作PDF文檔
這篇文章主要為大家詳細(xì)介紹了Java如何使用PDFBox實(shí)現(xiàn)操作PDF文檔,例如添加本地圖片、添加網(wǎng)絡(luò)圖片、圖片寬高自適應(yīng)、圖片水平垂直居中對(duì)齊等功能,需要的可以了解下2024-03-03Java獲取中文拼音、中文首字母縮寫(xiě)和中文首字母的示例
本文主要介紹了Java獲取中文拼音、中文首字母縮寫(xiě)和中文首字母,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-10-10解決idea使用過(guò)程中讓你覺(jué)得不爽的一些問(wèn)題(小結(jié))
這篇文章主要介紹了解決idea使用過(guò)程中讓你覺(jué)得不爽的一些問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08