我在使用Eclipse Kepler IDE的java中執行非常小的程序。java.lang.OutOfMemoryError:分配數組大小時的Java堆空間
我在運行時使用下面的語句分配數組的大小。
unSortedArray = new int[sizeRow][sizeColumn];
所以根據我的知識,java程序運行一些像32 MB一樣的自己的堆空間。
而對於我的程序,我認爲這個空間就夠了。
我剛剛在運行時給出了值sizeRow=3
和sizeColumn=3
。
當我按下進入它提供了以下異常java.lang.OutOfMemoryError: Java heap space
我把這個問題理解爲什麼會發生,以及我們如何處理呢?
我已閱讀以下鏈接。
java.lang.OutOfMemoryError: Java heap space
How to deal with "java.lang.OutOfMemoryError: Java heap space" error (64MB heap size)
編輯: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();
}
}
在我的情況下,我調用這些方法只有一次。
這些樣的錯誤無法處理,但我不知道爲什麼發生這種情況,你可以一次發佈完整的代碼。 – Batty
所以你創建了一個3x3的'int'數組,然後立刻得到一個OOME?真? – Vidya
是啊..這就像太陽圍繞地球旋轉:P,但必須有一些趕上! – Batty