从文件中读取 - 堆满了

时间:2018-05-03 13:57:58

标签: java heap-memory stringbuffer

我正在尝试从文件中读取并将它们放在哈希地图中。当我正在阅读一个巨大的文件或只有一个句子的文件时,它可以正常工作,但是当我正在阅读这两个文件时它会抛出

  

线程“main”中的异常java.lang.OutOfMemoryError:Java堆空间

在放入hashmaps之前,我将文本放入StringBuffer。它是StringBuffer使堆满了吗? 我的代码是:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

public class testforso {
public static void main(String args[]) throws FileNotFoundException {
    Scanner sc = new Scanner(System.in);
    ArrayList<String> fileNames = new ArrayList<String>();
    ArrayList<ArrayList<ArrayList<String>>> listWithFiles = new 
ArrayList<ArrayList<ArrayList<String>>>();
    String fileName;

    System.out.println("Enter the file names\t(Type stop to exit):");
    fileName = sc.next();
    while (!fileName.equals("stop")) {
        fileNames.add(fileName);
        fileName = sc.next();
    }

    for (int i = 0; i < fileNames.size(); i++) {
        Litourgia1 file = new Litourgia1(fileNames.get(i));
        listWithFiles.add(file.getWords());
    }
    System.out.println("Files read successfully");

    HashMap<String, HashMap<String, Integer>> hm = new HashMap<String, HashMap<String, Integer>>();
    HashMap<String, Integer> hm2 = new HashMap<String, Integer>();
    for (int i = 0; i < listWithFiles.size(); i++) { // for loop to get into files
        for (int j = 0; j < listWithFiles.get(i).size(); j++) { // for loop to get into sentences
            for (int k = 0; k < listWithFiles.get(i).get(j).size(); k++) { // for loop to get into words
                if (hm.containsKey(listWithFiles.get(i).get(j).get(k)))
                    hm2.put(listWithFiles.get(i).get(j).get(k), hm2.get(listWithFiles.get(i).get(j).get(k)) + 1);
                else
                    hm2.put(listWithFiles.get(i).get(j).get(k), 1);

            }
            for (int k = 0; k < listWithFiles.get(i).get(j).size(); k++) { // for loop to get into words
                if (!hm.containsKey(listWithFiles.get(i).get(j).get(k))) {
                    hm.put(listWithFiles.get(i).get(j).get(k), hm2);
                }
            }
        }
    }
    System.out.println(hm.toString()); //the Exception is in this line
}

}

由于

1 个答案:

答案 0 :(得分:-1)

当您的JVM耗尽堆内存时,抛出

OutOfMemoryError

如果你的JVM的堆大小为512MB并且你正在尝试读取1GB文件,那么你会得到这条消息,因为没有足够的堆内存分配给你的JVM。

要增加它,可以在运行java程序时使用以下参数:

2018-05-03 14:45:12.999  WARN 6336 --- [nio-9092-exec-5] o.hibernate.hql.internal.QuerySplitter   : HHH000183: no persistent classes found for query class: Select e from entity.AppUser e  Where e.userName = :userName 
2018-05-03 14:45:13.075  INFO 6336 --- [nio-9092-exec-5] o.h.h.i.QueryTranslatorFactoryInitiator  : HHH000397: Using ASTQueryTranslatorFactory

Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
    at org.hibernate.internal.AbstractSharedSessionContract.resultClassChecking(AbstractSharedSessionContract.java:703) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:687) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:23) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_05]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_05]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_05]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_05]
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:304) ~[spring-orm-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at com.sun.proxy.$Proxy75.createQuery(Unknown Source) ~[na:na]
    at dao.AppUserDAO.findUserAccount(AppUserDAO.java:24) ~[classes/:na]
    at dao.AppUserDAO$$FastClassBySpringCGLIB$$62f41262.invoke(<generated>) ~[classes/:na]
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:747) ~[spring-aop-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) ~[spring-tx-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor$$Lambda$375/10311291.proceedWithInvocation(Unknown Source) ~[na:na]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294) ~[spring-tx-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) ~[spring-tx-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689) ~[spring-aop-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at dao.AppUserDAO$$EnhancerBySpringCGLIB$$e269dd5.findUserAccount(<generated>) ~[classes/:na]

以下内容应将最大Java堆大小设置为2048MB或2GB。

e.g:

-Xmx2048M

如果您使用的是IDE,那么您应该可以配置java -jar -Xmx2048M someproject.jar ,在这种情况下,您只需添加program arguments