【问题标题】:Read Big file in Java , too slow and gc overhead limit exceeded [duplicate]读取 Java 中的大文件,速度太慢且超出 gc 开销限制 [重复]
【发布时间】:2016-06-10 16:14:07
【问题描述】:

我有一个大文件(大约 3GB)并读入一个 ArrayList 当我运行下面的代码时,几分钟后代码运行速度非常慢并且 CPU 使用率很高。 几分钟后 Eclipse 控制台显示错误 java.lang.OutOfMemoryError: GC 开销限制超出。

  • 操作系统:windows2008R2,
  • 4 杯,
  • 32GB 内存
  • java版本“1.7.0_60”

eclipse.ini

-startup
plugins/org.eclipse.equinox.launcher_1.3.0.v20130327-1440.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.200.v20140116-2212
-product
org.eclipse.epp.package.standard.product
--launcher.defaultAction
openFile
#--launcher.XXMaxPermSize
#256M
-showsplash
org.eclipse.platform
#--launcher.XXMaxPermSize
#256m
--launcher.defaultAction
openFile
--launcher.appendVmargs
-vmargs
-Dosgi.requiredJavaVersion=1.6
-Xms10G
-Xmx10G
-XX:+UseParallelGC
-XX:ParallelGCThreads=24
-XX:MaxGCPauseMillis=1000
-XX:+UseAdaptiveSizePolicy

java代码:

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("/words/wordlist.dat")));        
            InputStreamReader isr = new InputStreamReader(bis,"utf-8");
            BufferedReader in = new BufferedReader(isr,1024*1024*512);

            String strTemp = null;
            long ind = 0;

            while (((strTemp = in.readLine()) != null)) 
            {
                matcher.reset(strTemp);

                if(strTemp.contains("$"))
                {
                    al.add(strTemp);
                    strTemp = null;
                }
                ind = ind + 1;
                if(ind%100000==0)
                {
                    System.out.println(ind+"    100,000 +");
                }

            }
            in.close();

我的用例:

neural network
java
oracle
solaris
quick sort
apple
green fluorescent protein
acm
trs

【问题讨论】:

  • 你能详细说明你的用例吗?为什么内存中需要 3gb 的文件?
  • 是否需要将整个文件加载到内存中?
  • 你可以通过在eclipse配置中设置-XX:-UseGCOverheadLimi来暂时防止这个问题:disable-the-usegcoverheadlimit-in-centos
  • 为什么不将 JVM 堆大小(不是 Eclipse!)增加到有点像 6gb?你显然有足够的内存 ;)
  • @haraldK 但那是针对 Eclipse,而不是它启动的 JVM

标签: java performance bufferedreader


【解决方案1】:

用java编写一个程序来统计关键字在搜索词日志列表中被找到的次数

我建议你这样做。创建一个统计关键字出现次数的地图,或者所有单词的出现次数。

使用 Java 8 流,您可以在一两行内完成此操作,而无需一次将整个文件加载到内存中。

try (Stream<String> s = Files.lines(Paths.get("filename"))) {
    Map<String, Long> count = s.flatMap(line -> Stream.of(line.trim().split(" +")))
            .collect(Collectors.groupingBy(w -> w, Collectors.counting()));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    • 1970-01-01
    • 1970-01-01
    • 2011-05-21
    • 1970-01-01
    相关资源
    最近更新 更多