我正在完成同一任务。正在粘贴mycode。当我尝试运行10亿美元(即k = 10 ^ 9)的大型输入时,它显示运行时错误。你能帮我这个忙吗?

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Hashtable;
import java.util.StringTokenizer;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    static Long counter = (long)0;
    public static void main (String[] args) throws java.lang.Exception
    {
        BufferedReader ob1= new BufferedReader(new InputStreamReader(System.in));
        int user_choice= Integer.parseInt(ob1.readLine());

        int flag=0;
        while(user_choice!=0)
        {
            user_choice--;
            StringTokenizer obj;
            Long n,k;
            obj=new StringTokenizer(ob1.readLine());
            n=Long.parseLong(obj.nextToken());
            k=Long.parseLong(obj.nextToken());
            Hashtable<Long, Long> hash= new Hashtable<Long, Long>();
            while(n!=0)
            {
                StringTokenizer obj1;
                Long start,comp;
                Long finish;
                obj1=new StringTokenizer(ob1.readLine());
                start=Long.parseLong(obj1.nextToken());
                finish=Long.parseLong(obj1.nextToken());
                comp=Long.parseLong(obj1.nextToken());
                if(flag==0)
                {
                    for(int i=1;i<=k;i++)
                    {
                        hash.put((long)i,(long)0);
                    }

                }
                Long finish1;
                finish1=hash.get(comp);
                if(finish1==0)
                {
                    hash.put(comp,finish);
                    counter++;
                }
                else if(finish1<=start)
                {
                    counter++;
                    hash.put(comp,finish);
                }
                n--;
                flag=1;
            }
            flag=0;
            System.out.println(counter);
            counter=(long)0;
            /* for(int i=1;i<=k;i++)
      System.out.println(hash.get((long)i) +" "+i );*/

        }
    }
}


输入:

2
3 300000
1 3 1
4 6 2
7 10 3
3 10000000
1 3 1
4 6 2
7 10 3


输出:

Runtime error   time: 0.09 memory: 380160 signal:-1

3

最佳答案

这是捕获错误的Ideone源代码:http://ideone.com/w2Ub6Y

我不仅仅是throws Exception,而是这样做的:

System.out.println(Runtime.getRuntime().freeMemory() / 1000000.0 + "MB free");

try {

    // the whole program

} catch(Throwable t) {
    System.out.println(t.getClass().getName() + " " + t.getMessage());
    System.out.println();
    for(StackTraceElement elem : t.getStackTrace()) {
        System.out.println(elem);
    }
}


确实是OutOfMemoryError。输出为:

15.87436MB free
3
java.lang.OutOfMemoryError Java heap space

java.util.Hashtable.rehash(Hashtable.java:496)
java.util.Hashtable.put(Hashtable.java:560)
Ideone.main(Main.java:42)

Line 42 is hash.put:

hash.put((long)i,(long)0);


根据我的经验,〜16MB似乎是常见的“默认”堆大小。我很惊讶Ideone没有缩小它。如果要增大哈希表,则需要在本机IDE和increase the heap上运行它。如果需要,可以手动使堆很大。

Java堆应该根据需要自动增长,但是:


A)Ideone禁用它很聪明,似乎您正在使用Ideone(根据代码的模板外观判断),并且
B)如果堆增长太快,您仍然会收到OutOfMemoryError。

10-04 22:09