【发布时间】:2017-05-18 10:53:18
【问题描述】:
谢谢大家^_^,问题解决了:有一行太大(超过400M...我下载了一个损坏的文件,我没想到),所以抛出一个OutOfMemoryError
我想用java拆分文件,但总是抛出OutOfMemoryError: Java heap space,我在整个互联网上搜索,但似乎没有帮助:(
ps。文件大小为600M,超过30,000,000行,每行不超过100个字符。 (也许你可以像这样生成一个“级别文件”:{ id:0000000001,级别:1 id:0000000002,级别:2 ....(超过 3000 万) })
pss。将JVM内存大小设置得更大是行不通的,:(
psss。换了电脑,问题依旧/(ㄒoㄒ)/~~
无论我设置多大的-Xms或-Xmx,输出文件的大小总是一样的,(而且Runtime.getRuntime().totalMemory()确实改变了)
这是堆栈跟踪:
Heap Size = 2058027008
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2882)
at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:100)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:515)
at java.lang.StringBuffer.append(StringBuffer.java:306)
at java.io.BufferedReader.readLine(BufferedReader.java:345)
at java.io.BufferedReader.readLine(BufferedReader.java:362)
at com.xiaomi.vip.tools.ptupdate.updator.Spilt.main(Spilt.java:39)
...
这是我的代码:
package com.updator;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
public class Spilt {
public static void main(String[] args) throws Exception {
long heapSize = Runtime.getRuntime().totalMemory();
// Print the jvm heap size.
System.out.println("Heap Size = " + heapSize);
String mainPath = "/home/work/bingo/";
File mainFilePath = new File(mainPath);
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
try {
if (!mainFilePath.exists())
mainFilePath.mkdir();
String sourcePath = "/home/work/bingo/level.txt";
inputStream = new FileInputStream(sourcePath);
BufferedReader bufferedReader = new BufferedReader(new FileReader(
new File(sourcePath)));
String savePath = mainPath + "tmp/";
Integer i = 0;
File file = new File(savePath + "part"
+ String.format("%0" + 5 + "d", i) + ".txt");
if (!file.getParentFile().exists())
file.getParentFile().mkdir();
file.createNewFile();
outputStream = new FileOutputStream(file);
int count = 0, total = 0;
String line = null;
while ((line = bufferedReader.readLine()) != null) {
line += '\n';
outputStream.write(line.getBytes("UTF-8"));
count++;
total++;
if (count > 4000000) {
outputStream.flush();
outputStream.close();
System.gc();
count = 0;
i++;
file = new File(savePath + "part"
+ String.format("%0" + 5 + "d", i) + ".txt");
file.createNewFile();
outputStream = new FileOutputStream(file);
}
}
outputStream.close();
file = new File(mainFilePath + "_SUCCESS");
file.createNewFile();
outputStream = new FileOutputStream(file);
outputStream.write(i.toString().getBytes("UTF-8"));
} finally {
if (inputStream != null)
inputStream.close();
if (outputStream != null)
outputStream.close();
}
}
}
我想可能是:当 outputStream.close() 时,内存没有释放?
【问题讨论】:
-
显示异常和堆栈跟踪
-
查看以下已被其他人回答的链接-stackoverflow.com/questions/11578123/…
-
你为什么使用
Scanner?您不需要该功能,BufferedReader就足够了,而且资源消耗更少。 -
@FlameHaze,我试过了,但是无论我设置多大的-Xms或-Xmx,输出文件的大小总是一样的,(Runtime.getRuntime().totalMemory()是真的改变了)
-
堆栈很清楚:bufferedReader.readLine 抛出内存不足。要查找的最直接原因是:有一行不适合内存。 (您可以通过 System.out.println 行数来查看是哪一个)。
标签: java out-of-memory fileoutputstream