0

I am doing very small program in java using Eclipse Kepler IDE.

I am allocating the size of the array at run time using the following statement.

unSortedArray = new int[sizeRow][sizeColumn];

so as per my knowledge the java program runs with some of its own heap space like 32 MB.

And for my program i think this space is enough.

I just given the value sizeRow=3 and sizeColumn=3 at runtime.

And when i pressed enter it gives the following Exception java.lang.OutOfMemoryError: Java heap space

I put this question to understand why it happens and how we can handle it?

I already read the following link.

java.lang.OutOfMemoryError: Java heap space

How to deal with "java.lang.OutOfMemoryError: Java heap space" error (64MB heap size)

EDIT:1

public class ArraySort
{
private int[][]                 unSortedArray;
private int                     sizeColumn, sizeRow;

private final DataInputStream   dataInputStream = new DataInputStream(
                                                        System.in);

public boolean getArraySize() throws IOException
{
    try
    {
        System.out.print("Enter the size of the row:");
        sizeRow = (dataInputStream.readInt());
        System.out.print("Enter the size of the column:");
        sizeColumn = dataInputStream.readInt();
        return false;
    }
    catch (NumberFormatException e)
    {
        System.out.println("Enter valid value for size:");
    }
    return true;
}

public boolean getArrayElement() throws IOException
{
    unSortedArray = new int[sizeRow][sizeColumn];
    for (int i = 0; i < sizeRow; i++)
    {
        for (int j = 0; j < sizeColumn; j++)
        {
            try
            {
                System.out.println("Enter Element [" + i + "," + j + "]");
                unSortedArray[i][j] = dataInputStream.readInt();
            }
            catch (NumberFormatException ne)
            {
                System.out
                        .println("Enter proper Numeric value for Array Element");
                return true;
            }
        }
    }
    return false;
}
}


public class ArraySortProgram
{

public static void main(String[] args) throws IOException
{
    ArraySort arraySort = new ArraySort();

    arraySort.getArraySize();
    arraySort.getArrayElement();
}
}

In my case i am calling these method only once.

8
  • these kind of errors can not be handled, but I am not sure why this is happening, can you post complete code once. Commented Oct 26, 2013 at 6:40
  • So you create a 3x3 array of ints and get an OOME right after? Really? Commented Oct 26, 2013 at 6:40
  • yeah..this seems like sun revolving around earth :P but there has to be some catch!! Commented Oct 26, 2013 at 6:41
  • @Vidya yeah that is what i mean to say. Commented Oct 26, 2013 at 6:42
  • Could you please post some code? Are you sure you are initializing the array with those values? Commented Oct 26, 2013 at 6:43

2 Answers 2

2

The problems is here:

dataInputStream.readInt()

This would read a binary value from your input which would be like 2323827382 for your entered "1251" causing the OOM.

Try to use Scanner to read your integers like this:

public class ArraySort{
    private final Scanner scanner = new Scanner(System.in);

    int sizeRow, sizeColumn;

    public boolean getArraySize() throws IOException
    {
        try
        {
            System.out.print("Enter the size of the row:");
            sizeRow = (scanner.nextInt());
            System.out.print("Enter the size of the column:");
            sizeColumn = scanner.nextInt();
            return false;
        }
        catch (NumberFormatException e)
        {
            System.out.println("Enter valid value for size:");
        }
        return true;
    }    
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

1

If the offending code is as you have shown us, then there are possibilities:

  • Previous to executing this statement, you have allocated other objects and you have consumed nearly all of the available space in doing so. This statement is the proverbial straw that breaks the camel's back.

  • The values of sizeRow and sizeColumn are NOT what you think they are.

We cannot distinguish between these two possibilities without seeing more of your code.


UPDATE - Andrey has found the problem, based on your updated code. You are reading the sizes incorrectly, resulting in incorrect / very large size values. That is causing your application to try to allocate a HUGE array of arrays ... which is giving the OOME.

2 Comments

ya you are right but in my case i am calling these method only once.
See EDIT in my question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.