I was trying to save one of the class object to a File using ObjectOutputStream. When My object grows with the size I get the below error otherwise it is all good.
Exception in thread “Thread-2” java.lang.OutOfMemoryError: Java heap space
at java.io.ObjectOutputStream$HandleTable.growEntries(ObjectOutputStream.java:2308)
My code looks like this:
try {
FileOutputStream fout = new FileOutputStream("D:\\out.dat");
ObjectOutputStream os = new ObjectOutputStream(fout);
os.writeObject(this.obj); // Writing object to a File
os.close();
fout.close();
os=null;
fout=null;
} catch (Exception e) {
e.printStackTrace();
}
Please suggest me how can I resolve this? Any alternate approach to achieve the same?
Start your program with the
-Xms1024M -Xmx1024Moptions. You don’t usually get that error unless that object is really really big and/or you have a computer with a small RAM amount.