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 ...
随机推荐
- jsoup的elements类
jsoup的Elements类 一.简介 该类是位于select包下,直接继承自Object,所有实现的接口有Cloneable, Iterable<Element>, Collectio ...
- LAMT基于mod_proxy方式的负载均衡集群
一.apache服务器 # httpd -D DUMP_MODULES | grep proxy查看是否有 proxy_balancer_module (shared)模块 二.编辑配置文件 1.编 ...
- 课堂所讲整理:HTML--7JavaScript的DOM操作
1.DOM的基本概念 DOM是文档对象模型,这种模型为树模型:文档是指标签文档:对象是指文档中每个元素:模型是指抽象化的东西. 2.Window对象操作 一.属性和方法: 属性(值或者子对象): op ...
- 【原创】Algorithms:原地归并排序
第一次归并: a[0] a[1] a[2] a[3] a[4] a[5] a[6] 23 8 19 33 15 6 27 ↑ ↑ i j 最开始i指向a[0],j指向a ...
- Unity资源管理与更新
当你在 工程目录下的 Asset 文件夹中放置一个文件时(电脑上的文件夹,不是 Unity 界面中的文件夹),Unity会自动检测到一个新的文件被添加(Unity会不停地检查Assets文件夹中的内容 ...
- Unity3d 无网络权限下打开网站
有人问“更多游戏”没有网络权限怎么实现,其实调用浏览器访问外部链接不需要网络通讯权限,代码如下: Uri moreGame = Uri.parse("http://wapgame.189.c ...
- python3倒叙字符串
google测试工程师的一道题: 设计一个函数,使用任意语言,完成以下功能: 一个句子,将句子中的单词全部倒排过来,但单词的字母顺序不变.比如,This is a real world,输出结果为 w ...
- hadoop-2.7.0
65 cd /home/guyumei/下载/ 66 ll 67 cd .. 68 ll 69 cd .. 70 ll 71 cd guyumei/ 72 ll 73 cd hadoop-2.7.0/ ...
- generator函数
function* helloWordGenerator() { yield "hello"; yield "world"; return "endi ...
- mysql 查看最大连接数 设置最大连接数
方法一:进入MYSQL安装目录 打开MYSQL配置文件 my.ini 或 my.cnf查找 max_connections=100 修改为 max_connections=1000 服务里重起MY ...