user2845399
user2845399

Reputation: 111

Runtime Error for large Input

I am working on the same task.I am pasting mycode. Its show runtime error when i am trying to run for large input like 1 billion (i.e. k=10^9). Could you please help me with this?

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 );*/

        }
    }
}

Input :

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

Output:

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

3

Upvotes: 0

Views: 891

Answers (1)

Radiodef
Radiodef

Reputation: 37845

Here's an Ideone source code that catches the error: http://ideone.com/w2Ub6Y

Instead of just throws Exception I did this:

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);
    }
}

It's indeed an OutOfMemoryError. The output is:

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 seems to be a common "default" heap size in my experience. I am surprised Ideone doesn't make it smaller. If you want to make your Hashtable bigger you'd need to run it on a native IDE and increase the heap. You can manually make the heap very large if you want.

The Java heap is supposed to grow itself automatically as needed but:

  • A) Ideone would be smart to disable that and seems like you are using Ideone (judging by the template-look of your code) and
  • B) You can still get an OutOfMemoryError if the heap grows too fast.

Upvotes: 2

Related Questions