BufferMap映射复制文件,出异常,内存溢出!!!

kyousuke 2010-05-14 01:15:37
下面的程序是使用映射文件ByteBufferMap来复制一个文件,每次复制到大概7,8百M的时候就出内存溢出异常
大家看看是什么原因啊?我的电脑内存是2G,每次映射文件的大小是128M
复制的文件大小是1.3G

java.io.IOException: Map failed
at sun.nio.ch.FileChannelImpl.map(Unknown Source)
at iotest.ByteBufferMap.main(ByteBufferMap.java:38)
Caused by: java.lang.OutOfMemoryError: Map failed
at sun.nio.ch.FileChannelImpl.map0(Native Method)
... 2 more



import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;

public class ByteBufferMap {

public static void main(String[] args) {
FileChannel fcIn = null;
FileChannel fcOut = null;
try {
String filePath = "F:\\Anime\\Gundam00\\S2\\25.mkv";//1.3G
fcIn = new FileInputStream(new File(filePath)).getChannel();
fcOut = new RandomAccessFile(new File("G:\\1.mkv"), "rw").getChannel();
IntBuffer bufOut;
IntBuffer bufIn;

long readSize = 128 * 1024 * 1024;//每次映射的大小
long s;//映射开始处
long e;//映射结束处
int i = 0;
long allSize = fcIn.size();//文件的大小

long start = System.currentTimeMillis();

do {
s = i++ * readSize;
e = s + readSize > allSize ? s + allSize % readSize : s + readSize;
bufIn = fcIn.map(FileChannel.MapMode.READ_ONLY, s, e).asIntBuffer();
bufOut = fcOut.map(FileChannel.MapMode.READ_WRITE, s, e).asIntBuffer();

while(bufIn.hasRemaining()) {
bufOut.put(bufIn.get());
}

} while(e < allSize);

long end = System.currentTimeMillis();
System.out.println("Time:" + ((double) ((end - start) / 1000)) + "s");

} catch(IOException e) {
e.printStackTrace();
} finally {
try {
fcIn.close();
fcOut.close();
} catch (IOException e) {
e.printStackTrace();
}

}
}
}

...全文
639 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
A_S478 2013-01-15
  • 打赏
  • 举报
回复
java.io.IOException: 拒绝访问。 at sun.nio.ch.FileChannelImpl.truncate0(Native Method) at sun.nio.ch.FileChannelImpl.map(FileChannelImpl.java:731) at com.gdcn.download.http.DownFileHelper.downFile(DownFileHelper.java:132) 代码: bytes= new byte[(int)(outSize-startsSize)]; 错误行: MappedByteBuffer out =channel.map(MapMode.READ_ONLY,startsSize,outSize); out.get(bytes); //获取bytes大小的字符流加载到bytes数组中 if(bytes != null) cleanMappedByteBuffer.clean(out); 如今我也出现这问题了
mediagao 2011-06-27
  • 打赏
  • 举报
回复
这段代码还是会出现:java.io.IOException: Map failed。
kyousuke 2010-05-14
  • 打赏
  • 举报
回复

package iotest;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;

public class ByteBufferMap {

public static void main(String[] args) {
FileChannel fcIn = null;
FileChannel fcOut = null;
try {
String filePath = "F:\\Anime\\Gundam00\\S2\\25.mkv";
fcIn = new FileInputStream(new File(filePath)).getChannel();
fcOut = new RandomAccessFile(new File("G:\\1.mkv"), "rw").getChannel();
IntBuffer bufOut;
IntBuffer bufIn;

int readSize = 256 * 1024 * 1024;//每次映射的大小
long s;//映射开始处
long e;//映射结束处
int i = 0;
long allSize = fcIn.size();
System.out.println(Integer.MAX_VALUE + " " + readSize);

long start = System.currentTimeMillis();

do {
s = i++ * readSize;
e = s + readSize > allSize ? allSize % readSize : readSize;
/*System.out.println(s);
System.out.println(e);
System.out.println(allSize + "\n");*/
bufIn = fcIn.map(FileChannel.MapMode.READ_ONLY, s, e).asIntBuffer();
bufOut = fcOut.map(FileChannel.MapMode.READ_WRITE, s, e).asIntBuffer();

while(bufIn.hasRemaining()) {

bufOut.put(bufIn.get());
}

} while(s + e < allSize);

long end = System.currentTimeMillis();
System.out.println("Time:" + ((double) ((end - start) / 1000)) + "s");

} catch(IOException e) {
e.printStackTrace();
} finally {
try {
fcIn.close();
fcOut.close();
} catch (IOException e) {
e.printStackTrace();
}

}
}
}

soli11722984 2010-05-14
  • 打赏
  • 举报
回复
LZ把正确答案的代码都发上来
kyousuke 2010-05-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jxsfreedom 的回复:]
找到原因了,我把map的第三个参数理解错了
现在可以复制完了,最后出了这个异常,不知道怎么回事

Java code

java.io.IOException: 拒绝访问。
at sun.nio.ch.FileChannelImpl.truncate0(Native Method)
at sun.nio.ch.FileChannelImpl.map(Unknown S……
[/Quote]

又找到原因 了,- -!自言自语了,大家无视我好了
kyousuke 2010-05-14
  • 打赏
  • 举报
回复
找到原因了,我把map的第三个参数理解错了
现在可以复制完了,最后出了这个异常,不知道怎么回事

java.io.IOException: 拒绝访问。
at sun.nio.ch.FileChannelImpl.truncate0(Native Method)
at sun.nio.ch.FileChannelImpl.map(Unknown Source)
at iotest.ByteBufferMap.main(ByteBufferMap.java:34)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧