[jdom-interest] Re: Memory problems

Pramodh Peddi peddip at contextmedia.com
Mon Dec 29 14:04:23 PST 2003


The method I gave in my previous email is used when I have a String. This is the way I am build the document when I already have the InputStream. And I am encountering the OutOfMemoryError problem when I am using this method. I would assume the earlier method would definitely give the same problem (because it has to convert String to byte[] and then prepare the InputStream from that).

This is the method:

***********************************************************
 public Document getDocumentFromStream(InputStream xmlStream)
  throws Exception {
  Document doc = null;

  BufferedReader reader = null;

  try {
   reader =
    new BufferedReader(
     new InputStreamReader(xmlStream, "ISO-8859-1"));
  } catch (UnsupportedEncodingException e) {
   log.warn("Character set Cp1252 not supported, defaulting to UTF-8");
   reader =
    new BufferedReader(new InputStreamReader(xmlStream, "UTF8"));
  }

  try {
   SAXBuilder builder = new SAXBuilder(false);

   doc = builder.build(reader);

   return doc;

  } catch (JDOMException e) {
   log.error("Error parsing file:", e);
   throw e;
  }
 }
*********************************************************
  ----- Original Message ----- 
  From: Pramodh Peddi 
  To: jdom-interest at jdom.org 
  Sent: Monday, December 29, 2003 4:48 PM
  Subject: Memory problems


  Hi,
  I am using JDOM for XML processing. I use SAXBuilder. I am having memory problems when it deals with huge documents (like 10MB), even though it is using SAXBuidler. It throws OutOfMemoryError when I try to build a 8MB document.
  I assume it does not keep the whole tree in memory if I use SAXBuilder. Does it? If what I assume is right, why is it running out of memory while building the doc of 8MB xml? 8MB should not be enough to make fail because of memory problems. right?

  This is how I build a document:
  ****************************************************************************************************
      public Document getDocumentfromString(String xmlString)
          throws JDOMException, IOException {
          Document schemaDoc = null;
          SAXBuilder builder = new SAXBuilder(false);
          String resultingXML = null;
          byte[] xmlBytes = null;

          try {
              //get the UTF 8 encoded bytes
              xmlBytes = xmlString.getBytes("UTF8");
              schemaDoc = builder.build(new ByteArrayInputStream(xmlBytes));
          } catch (UnsupportedEncodingException usee) {
              //   system does not support UTF-8 encoding. use default encoding   
              schemaDoc = builder.build(new StringReader(xmlString));
          }

          return schemaDoc;
      }
  *****************************************************************************************************

  Am I doing anything wrong to make it fail while building a 8MB doc?

  Do you think using JAXP API (again, using SAX) is better memory wise?

  Any help would be greatly appreciated!

  Thanks,

  Pramodh.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20031229/e1668b0d/attachment.htm


More information about the jdom-interest mailing list