java.lang.OutOfMemoryError: Java heap space

Hello All,

we have application which is on swings,

in our application we have module called Report Generation

here in Report module we are generating the report and writing or saving it in.XML extension file.

for writing.XMl extension is the following is the code

writingReport(){

String filename = System.getProperty("user.home") +"/Report.xml";

System.out.println("XML Report file path" +filename);

File myfile =new File(filename);

FileWriter mywriter =null;

BufferedReader reader =null;

try{

mywriter =new FileWriter(myfile);

System.out.println("Now I'm reading each line from Online-Report and Displaying each line");

// Reading from Online-Report .

reader =new BufferedReader(new InputStreamReader(conn.getInputStream()));

// where 'conn' is URLConnection

String line ="";

System.out.println("*************Each Line Display from Online Report***************");

while ((line = reader.readLine()) !=null){

System.out.println(line);

mywriter.write(line);

//System.out.println("Each Line Display from Report.XML" +line);

}

System.out.println("Report has been written to " + myfile );

System.out.println(myfile +"is a Temproray file,deleted after once reading the content");

reader.close();

mywriter.close();

}catch(Exception e){

e.printStackTrace();

try{

reader.close();

mywriter.close();

}catch (IOException ex){

ex.printStackTrace();

}

}

}

For the above i'm getting following error

Exception in thread "Thread-6" java.lang.OutOfMemoryError: Java heap space

Output For above code:-

XML Report file pathC:\Documents and Settings\Administrator/Report.xml

Now I'm reading each line from Online-Report and Displaying each line

*************Each Line Display from Online Report***************

<?xml version="1.0" standalone="yes"?>

Exception in thread "Thread-6" java.lang.OutOfMemoryError: Java heap space

1: How can we increase the heap memory?

2: What is Default size of the BufferedReader ? is there any possiblity ofincreasing the BufferedReader ?

Please acknoweldge me...

Thanks in advance

Anil Patil

[3370 byte] By [anilrpa] at [2007-11-27 4:39:41]
# 1

First I'll like to know what database you are using.

Out of memory error can come because of two reasons :

1. The report file u made was larger than the heap size available.

2. The heap space of the database is less than the file size ( in case

you are using a xml database e.g. XStreamDB)

You can increase the xms and xmx parameters of java to increase the heap space.

Also i'll like to know what IDE u are using.

Anand.Aa at 2007-7-12 9:50:26 > top of Java-index,Java Essentials,Java Programming...
# 2

Hi Anand

I'm not using any local database, its from the server(soory i cant mention servername ) consider any X server , where thousands of report's data available for which i'm reading as reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

how to know the initial Heap size?

i'm using the Netbeans 5.5 IDE

anilrpa at 2007-7-12 9:50:26 > top of Java-index,Java Essentials,Java Programming...
# 3
And one more thing i for to mention,i tried to increase Heap memory at command prompt which goes like thisC:\Myapplication>java -Xms64 -Xmx150mError occurred during initialization of VMToo small initial heaphow to know that initial heap?
anilrpa at 2007-7-12 9:50:26 > top of Java-index,Java Essentials,Java Programming...
# 4
What is the entire stack trace?Is it possible that the second line of the report is actually the entire rest of the report? That is, there are no new line characters and therefore, the readLine() call is trying to read in a huge amount of data.
jbisha at 2007-7-12 9:50:26 > top of Java-index,Java Essentials,Java Programming...
# 5

I am well aware with this error as i had faced this in my previous project

where i used to build large XML files.

If your server is a windows machine with OS as 2000 or Server

then wahtever level you increase the xms and xms parameters you will

not be able to increase it as it has a fixed level.

If it is something else such as Xp or is linux machine then u can solve the issue by increasing the xms and xmx parameters.

Now, if u are usiong tomcat 5.5 or higher and using a windows machine

then u can set parameters in configure tomcat in the menu

or can config it using command prompt.

There are a few lines which can be used to see the current memory

level of the system :

System.out.println("freeMemory"+Runtime.getRuntime().freeMemory());

System.out.println(" totalMemory"+Runtime.getRuntime().totalMemory());

System.out.println(" maxMemory"+Runtime.getRuntime().maxMemory());

Also u must use a high memory configuration at the server i.e good RAM

I will give u a link where some help about the topic is available

which probably must be of help to you

http://www.informix-zone.com/node/46

Cheers

Enjoy...!!!!!

Anand.Aa at 2007-7-12 9:50:26 > top of Java-index,Java Essentials,Java Programming...
# 6
Hey Anand, thanks for all that long Reply , i'll check the things what you suggested hope it will work for me...
anilrpa at 2007-7-12 9:50:26 > top of Java-index,Java Essentials,Java Programming...
# 7

You had a too small memory heap when you use C:\Myapplication>java -Xms64 -Xmx150m because your first setting should be -Xms64M. On Windows OS java heap reserves at least about 16M as a start and you asked much less. You can find the same information in the link that Anand.A provided.

BD.

BinaryDecimala at 2007-7-12 9:50:26 > top of Java-index,Java Essentials,Java Programming...
# 8
Hey cheers buddy and letme know the result
Anand.Aa at 2007-7-12 9:50:26 > top of Java-index,Java Essentials,Java Programming...
# 9

Hello

I tried the following at cammand prompt

C:\my Application> java -Xms64m -Xmx 150m -jar application.jar

it went all fine i got what i required

Since i increased heap size i tried to re run apllication by following

C:\my Application> java-jar application.jar

but i got the same error "Exception in thread "Thread-6" java.lang.OutOfMemoryError: Java heap space "

Is it for every time we run the application we need to set the heap size?

is there any way to fix this ?

I am using the Windows2000 OS

anilrpa at 2007-7-12 9:50:26 > top of Java-index,Java Essentials,Java Programming...
# 10
Hey can u tell me the configuration of the server and application server u are using.Moreover just dump the SOP's i gave just before u make the fileAlso as i told if windows2000 is ur server OS then there is a problemlet me know
Anand.Aa at 2007-7-12 9:50:26 > top of Java-index,Java Essentials,Java Programming...
# 11
As far as I know you need to set the heap size every time. You can create a batch file with your command that includes the heap reservation. This way you write it only once.BD.
BinaryDecimala at 2007-7-12 9:50:26 > top of Java-index,Java Essentials,Java Programming...
# 12
Make sure you define where your JRE path is in the batch file.
BinaryDecimala at 2007-7-12 9:50:26 > top of Java-index,Java Essentials,Java Programming...
# 13
Please any body help me in fixing above thingsThanks
anilrpa at 2007-7-12 9:50:26 > top of Java-index,Java Essentials,Java Programming...
# 14
hey BinaryDecimal / All,thanks by making batch file its working...but we can't insist client's(customer) to run the batch file Is there any solution?please Acknowledge me...Thanks
anilrpa at 2007-7-12 9:50:26 > top of Java-index,Java Essentials,Java Programming...
# 15
I am sure there is a third party tool that can make a professional shortcut which itself points to the batch file. If your client doesn't want to use the shortcut, please specifiy what they want.BD.
BinaryDecimala at 2007-7-21 21:11:07 > top of Java-index,Java Essentials,Java Programming...