【问题标题】:Running out of memory Java EclipseJava Eclipse内存不足
【发布时间】:2013-10-23 13:21:47
【问题描述】:
public class Functions {

public Functions (){

}

Map<String, Set<String>> task() throws Exception{

    System.out.println("start reading for strain file");

    String value= "/home/charles/Documents/mmp.txt";
    File file = new File(value);


    FileInputStream fin = new FileInputStream(file);

    FileChannel fc = fin.getChannel();
    MappedByteBuffer mapByteBuff = fc.map(FileChannel.MapMode.READ_ONLY, 0, file.length());

    byte[] buffer= new byte[(int)fc.size()];
    mapByteBuff.get(buffer);
    BufferedReader br=new BufferedReader(new InputStreamReader(new ByteArrayInputStream(buffer)));
    Map<String, Set<String>> strainHashMap = new HashMap<String, Set<String>>(824*12+1,2);


    String line = br.readLine();

    //  String delim="[\t]";
    String[] tokens=new String[3];
    //      String delim="[\t]";

    for (line = br.readLine(); line != null; line = br.readLine()) {

        //          tokens=new String[3];
        tokens=line.split("[\t]",3);
                     //here is where I am stuck.. I get java out of heap memory

        Set<String> strainSet = strainHashMap.get(tokens[1]);
        if(strainSet==null){
            strainSet = new HashSet<String>();
            strainSet.add(tokens[2]);
            strainHashMap.put(tokens[1],strainSet);
        }
        else{
            strainSet.add(tokens[2]);
            strainHashMap.put(tokens[1], strainSet);
        }


        if(strainHashMap.size() % 600000 ==0){
            System.out.println("strain"+strainHashMap.size());
        }


        if(strainHashMap.size() % 610000 ==0){
            System.out.println("strain"+strainHashMap.size());
        }

        if(strainHashMap.size() % 620000 ==0){
            System.out.println("strain"+strainHashMap.size());
        }


        if(strainHashMap.size() % 650000 ==0){
            System.out.println("strain"+strainHashMap.size());
        }

        if(strainHashMap.size() % 700000 ==0){
            System.out.println("strain"+strainHashMap.size());
        }


        if(strainHashMap.size() % 851000 ==0){
            System.out.println("strain"+strainHashMap.size());
        }

    }

    fc.close();
    fin.close();
    System.out.println("The hash stain is " + strainHashMap.size());

    //  System.out.println("The Genotype is " + mapGenotype.size());

    System.out.println("moving out of strain function");
    return strainHashMap;

}

public Map<String, Genotype> genotype() throws Exception {
    // TODO Auto-generated method stub

    System.out.println("entering genotype data function");
    // TODO Auto-generated method stub
    String value= "/home/charles/Documents/mmp.txt";
    File file = new File(value);
    FileInputStream fin = new FileInputStream(file);
    FileChannel fc = fin.getChannel();
    MappedByteBuffer mapByteBuff = fc.map(FileChannel.MapMode.READ_ONLY, 0, file.length()); 

    byte[] buffer= new byte[(int)fc.size()];
    mapByteBuff.get(buffer);

    BufferedReader br=new BufferedReader(new InputStreamReader(new ByteArrayInputStream(buffer)));
    Map<String, Genotype> mapGenotype = new HashMap<String, Genotype>(824*12+1,2);

    String line = br.readLine();
    //  String delim="[\t]";
    String[] tokens=new String[8];
    //  String delim="[\t]";

    for (line = br.readLine(); line != null; line = br.readLine()) {
        tokens=new String[8];
        tokens=line.split("[\t]",8);

        //      tokens=line.split(delim);
        //      Genotype genotypeObject = new Genotype();

        //      genotypeObject.setChromsomeName(tokens[3]);

        //      genotypeObject.setMutation(tokens[6]);

        //      genotypeObject.setPosition(Double.parseDouble(tokens[4]));
        //genotypeObject.setPosition(Double.parseDouble(tokens[4]));

        //      genotypeObject.setRef(tokens[5]);

        if(!mapGenotype.containsKey(tokens[1])){


            Genotype genotypeObject = new Genotype();

            genotypeObject.setChromsomeName(tokens[3]);

            genotypeObject.setMutation(tokens[6]);

            genotypeObject.setPosition(Double.parseDouble(tokens[4]));
            //genotypeObject.setPosition(Double.parseDouble(tokens[4]));

            genotypeObject.setRef(tokens[5]);
            mapGenotype.put(tokens[1], genotypeObject);

        }

        if(mapGenotype.size() % 700000 ==0){
            System.out.println(mapGenotype.size());
        }


        if(mapGenotype.size() % 650000 ==0){
            System.out.println(mapGenotype.size());
        }

        if(mapGenotype.size() % 851000 ==0){
            System.out.println(mapGenotype.size());
        }

    }
    fc.close();
    fin.close();
    System.out.println("leaving Genotype method of function");
    return mapGenotype;

}

}

奇怪的是,hash map 在 task 方法中添加了 650000 个值后内存不足。 我用类似的文件运行相同的方法,它工作得非常好基因型

我正在使用 jdk 1.6 Ubuntu 12.04,Eclipse 开普勒。 .ini 文件有-XX:MaxPermSize=256m -Xms740m -Xmx1412m

运行方式配置如下: -Xms912m -Xmx2124m 交换内存超过 3.5 GB。

我可以在某个时候完成这项任务,但不幸的是,我丢失了所有数据,我正在尝试重新设置所有这些。 我花了很多时间来解决这个问题,但似乎没有任何帮助。

这是我得到的错误:Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

【问题讨论】:

    标签: java eclipse split hashmap


    【解决方案1】:

    您的 JVM 设置对我来说似乎很慷慨。我看不出把它们变大会有什么帮助。

    我是否正确理解了您的代码 - 您是否将整个文件读入内存,然后遍历所有行以解析出 Map 中的条目?

    如果是,我会重写它,一次只在内存中保留一行:读取一行,解析它,添加到 Map,重复。

    运行VisualVM 以查看运行时内存消耗的位置。可能会让人大开眼界。

    【讨论】:

      【解决方案2】:

      最可能的解释是 JVM 内存不足。为此,您需要知道 Xmx 设置是允许 Java 使用的上限无论您的主机可能有什么可用的。

      因此,您要么需要增加 Xmx 设置,例如 -Xmx4g,然后重试,要么重写代码以使用更少的内存。

      【讨论】:

      • 他说他的运行时配置是 2g。
      • 我终于不得不改变我的系统来解决这个问题。可能是我的 Wubi-Ubuntu 环境中的空间问题。不过不确定。
      【解决方案3】:

      确保您提供的内存参数设置在 VM 参数框中,而不是程序参数框中。

      我建议打印Runtime.getRuntime().maxMemory() 的值以找出(以字节为单位)Java 认为允许使用多少内存来检查 -Xmx 参数是否被接受,并定期打印Runtime.getRuntime().totalMemory() 的值查看您的数据在崩溃之前占用了多少内存以及正在使用多少内存。

      【讨论】:

        【解决方案4】:

        我认为您超出了“PermGen”大小。设置为“-XX:MaxPermSize=912m”。

        Perm 空间用于保存已加载类的信息以及一些其他高级功能(如字符串池)。参考:What is perm space?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-06-21
          • 2013-09-10
          • 2012-12-12
          • 2017-07-19
          • 1970-01-01
          • 1970-01-01
          • 2014-08-02
          • 1970-01-01
          相关资源
          最近更新 更多