OutOfMemoryError occured

hi,

here in my code i am using SAX to read and DOM to write.

i am passing destination file path and the document object.

i am trying to save xml file 50,000 subroot records with in the root in this fashion.

<Root>

<subroot>

<FirstElement>1</FirstElement>

<SecondElement>2</SecondElement>

<ThirdElement>3</thirdElement>

<ForthElement>4</ForthElement>

<FifthElement>5</FifthElement>

</subroot>

</root>

while transforming DOMSource to StreamResult i got OutOfMemoryError.

here is the following code.

can any one help.

privatevoid transformAndSave(File theDestinationFile, Document theDocument)

throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException, IOException{

TransformerFactory transformerFactory = TransformerFactory.newInstance();

Transformer transformer = transformerFactory.newTransformer();

try{

transformer.setOutputProperty(OutputKeys.INDENT,"yes");

transformer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");

transformer.setOutputProperty(OutputKeys.STANDALONE,"yes");

transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","4");

}catch (Exception e){

// We don't want to fail just because we are unable to set a property (incase we are not using the default one).

// So, just ignore any exceptions thrown.

}

// Create a StringWriter to store the transformed result (XML document)

StringWriter stringWriter =new StringWriter();

StreamResult streamResult =new StreamResult(stringWriter);

// Create a Source object for the Document which we want to transform.

DOMSource source =new DOMSource(theDocument);

transformer.transform(source, streamResult);

// here i have encounterd (OutOfMemoryError)

// Now we have the XML contents in the StringWriter (which we passed to the StreamResult)

// Write the XML contents to the file now.

BufferedWriter bufferedWriter =new BufferedWriter(new FileWriter(theDestinationFile));

bufferedWriter.write(stringWriter.toString());

bufferedWriter.flush();

// Close the Writer now.

try{

bufferedWriter.close();

}catch (Exception e){

// We don't want to throw an exception if the close() fails.

// Just ignore the exception thrown during close()

}

}

thank you

[3656 byte] By [Inspiron123a] at [2007-11-26 17:28:05]
# 1

this is because ur buffer memory can't store so large data....u need high memory configuration. check ur RAM size and if ur using eclipse then go to eclipse folder ..check the eclipse cofig file u can see like that..

-vmargs

-Xms40m

-Xmx256m

...

that is the min and max limit of buffer memory..u can try with incresing that..

debduttaa at 2007-7-8 23:56:01 > top of Java-index,Java Essentials,New To Java...
# 2
hithanks for the reply.but thing is what u have suggested. i have checked its all the same.(512 RAM)thank you.
Inspiron123a at 2007-7-8 23:56:01 > top of Java-index,Java Essentials,New To Java...
# 3

Consider:

1. How much RAM is in your PC?

2. How much RAM are you allowed to use on your production server?

DOM uses approximately 3.5 times (it's improving) the size of the file on disk.

50,000 lines by say 132 characters on a line.... Hmmm.... Don't Use DOM!

Keith.

corlettka at 2007-7-8 23:56:01 > top of Java-index,Java Essentials,New To Java...