Skip to content
Advertisement

java.lang.OutOfMemoryError: Java heap space in allocating array size

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.

Advertisement

Answer

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

Advertisement