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.

trincot
  • 317,000
  • 35
  • 244
  • 286
Nirav Kamani
  • 3,192
  • 7
  • 39
  • 68

2 Answers2

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;
    }    
    ...
}
Andrey Chaschev
  • 16,160
  • 5
  • 51
  • 68
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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216