MappedByteBuffer读写文件
一. MappedByteBuffer
java把文件映射到内存中,避免堆内存产生大对象引起full gc。mappedByteBuffer的读写速度都要超过堆内读写文件的速度
public class AA {
public static void main(String[] args) throws IOException, InterruptedException {
map();
//fis();
} private static void fis() throws IOException {
/*long l = System.currentTimeMillis();
FileOutputStream fos = new FileOutputStream(new File("d://1234.txt"));
for(int i=0;i<4000000;i++)
fos.write('a');
System.out.println((System.currentTimeMillis()-l)); //13011*/ long l = System.currentTimeMillis();
FileInputStream fis = new FileInputStream(new File("d://1234.txt"));
int read;
for(int i=0;i<4000000;i++) {
read = fis.read();
}
System.out.println((System.currentTimeMillis()-l)); //9774
} private static void map() throws IOException {
/*long l = System.currentTimeMillis();
FileChannel fileChannel = new RandomAccessFile("d://1234.txt", "rw").getChannel();
CharBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, 4000000*2).asCharBuffer();
System.out.println("start");
for(int i=0;i<4000000;i++)
mappedByteBuffer.put('a');
System.out.println((System.currentTimeMillis()-l));
fileChannel.close(); //47*/ long l = System.currentTimeMillis();
FileChannel fileChannel = new RandomAccessFile("d://1234.txt", "r").getChannel();
CharBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size()).asCharBuffer();
System.out.println("start");
for(int i=0;i<4000000;i++)
mappedByteBuffer.get();
System.out.println((System.currentTimeMillis()-l));
fileChannel.close(); //40
}
}关闭MappedByteBuffer
public final class IOUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(IOUtil.class); private IOUtil() {
} public static void closeMappedByteBuffer(final MappedByteBuffer byteBuffer) {
if (byteBuffer == null) {
return;
} if (byteBuffer != null) {
AccessController.doPrivileged(
new PrivilegedAction<Object>() {
@Override
public Object run() {
try {
Method cleanerMethod = byteBuffer.getClass().getMethod("cleaner", new Class[0]);
cleanerMethod.setAccessible(true);
sun.misc.Cleaner cleaner = (sun.misc.Cleaner) cleanerMethod.invoke(byteBuffer,
new Object[0]);
cleaner.clean();
} catch (NoSuchMethodException e) {
LOGGER.error("error occurred when clean mapped byte buffer", e);
} catch (InvocationTargetException e) {
LOGGER.error("error occurred when clean mapped byte buffer", e);
} catch (IllegalAccessException e) {
LOGGER.error("error occurred when clean mapped byte buffer", e);
}
return null;
}
}
);
}
}
}
MappedByteBuffer读写文件的更多相关文章
- RandomAccessFile、FileChannel、MappedByteBuffer读写文件
s package com.nio; import java.io.Closeable; import java.io.FileNotFoundException; import java.io.IO ...
- 顺序、随机IO和Java多种读写文件性能对比
概述 对于磁盘的读写分为两种模式,顺序IO和随机IO. 随机IO存在一个寻址的过程,所以效率比较低.而顺序IO,相当于有一个物理索引,在读取的时候不需要寻找地址,效率很高. 基本流程 总体结构 我们编 ...
- Hyper-V无法文件拖拽解决方案~~~这次用一个取巧的方法架设一个FTP来访问某个磁盘,并方便的读写文件
异常处理汇总-服 务 器 http://www.cnblogs.com/dunitian/p/4522983.html 服务器相关的知识点:http://www.cnblogs.com/dunitia ...
- 计算机程序的思维逻辑 (60) - 随机读写文件及其应用 - 实现一个简单的KV数据库
57节介绍了字节流, 58节介绍了字符流,它们都是以流的方式读写文件,流的方式有几个限制: 要么读,要么写,不能同时读和写 不能随机读写,只能从头读到尾,且不能重复读,虽然通过缓冲可以实现部分重读,但 ...
- Python读写文件
Python读写文件1.open使用open打开文件后一定要记得调用文件对象的close()方法.比如可以用try/finally语句来确保最后能关闭文件. file_object = open('t ...
- php中并发读写文件冲突的解决方案
在这里提供4种高并发读写文件的方案,各有优点,可以根据自己的情况解决php并发读写文件冲突的问题. 对于日IP不高或者说并发数不是很大的应用,一般不用考虑这些!用一般的文件操作方法完全没有问题.但如果 ...
- C#读写文件的方法汇总_C#教程_脚本之家
C#读写文件的方法汇总_C#教程_脚本之家 http://www.jb51.net/article/34936.htm
- Inno Setup 如何读写文件
软件安装的实质就是拷贝,对于简单的打包当然不需要考虑修改某(配置)文件.通过inno修改文件的目的在于把安装时相关信息写入文件中,提供其它应用的读取,而这些信息也只能在安装时才能确定,比如安装用户选择 ...
- java使用IO读写文件总结
每次用到IO的读写文件都老忘记写法,都要翻过往笔记,今天总结下,省的以后老忘.java读写文件的IO流分两大类,字节流和字符流,基类分别是字符:Reader和Writer:字节:InputStream ...
随机推荐
- ScrollView和ListView的冲突问题
在ScrollView添加一个ListView会导致listview控件显示不全,这是因为两个控件的滚动事件冲突导致.所以需要通过listview中的item数量去计算listview的显示高度,从而 ...
- 【转】beancopy的替代方案
链接:http://jingyan.baidu.com/article/215817f7d55b871edb14235b.html 最近在项目中接触到了BeanUtils.copyProperties ...
- 【转】 WebService到底是什么?
WebService到底是什么? http://blog.csdn.net/wooshn/article/details/8069087/
- 工作中遇到的问题--实现CustomerSetting的实时更新
首先在项目运行时就初始化CustomerSettings的值,采用@Bean,默认是singtone模式,只会加载一次. @Configuration@Order(3)@EnableWebMvcSec ...
- Codeforces Round #127 (Div. 2)
A. LLPS 长度最大10,暴力枚举即可. B. Brand New Easy Problem 枚举\(n\)的全排列,按题意求最小的\(x\),即逆序对个数. C. Clear Symmetry ...
- spark与storm的对比
对比点 Storm Spark Streaming 实时计算模型 纯实时,来一条数据,处理一条数据 准实时,对一个时间段内的数据收集起来,作为一个RDD,再处理 实时计算延迟度 毫秒级 秒级 吞吐量 ...
- swift与OC混编高级教程之混编框架的创建和调用
首先创建一个project取个名字叫“MyMixed”,选择iOS-framework&library-cocoa touch framework 然后在里面创建一个SwiftView.swi ...
- 使用a标签直接下载图片
通常情况下,使用a标签链接到图片,会在浏览器中打开这个图片,而不会下载 如果要直接下载这个图片,可以使用download属性配合href属性 <a href="./1.jpg" ...
- 论文笔记之:Fully Convolutional Attention Localization Networks: Efficient Attention Localization for Fine-Grained Recognition
Fully Convolutional Attention Localization Networks: Efficient Attention Localization for Fine-Grain ...
- PADS Logic 常见错误报告内容
1.PCB Decal LED0805 not found in Library pcb封装不在库中. 找到原图中的pcb-save to library 未分配PCB时候,右键Edit part-找 ...