Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 881
  • Last Modified:

xmlbeans out of memory error.

Hello,

        I need to genrate a large XML file. I am using xmlbeans for genration. It works fine when i have to genrate a small file of upto 10,000 records but for my use I need as many as over 50,000 records for which I am getting this out of memory error.
                 
                The way I am doing it is  I keep genrating xml file in a while loop as long as I am haivng records in resultSet when there is no more data in result set after the while loop i write it to file using save(outputstream) (XMLTokenSource) .

 I am hoping if there is a way where i can keep writing to a file as I am adding new elements to the XML there by it does not have to build the tree in the memory and write all at once at the end.

Sunitha

0
sunithas
Asked:
sunithas
  • 2
  • 2
1 Solution
 
MogalManicCommented:
Could you send a code sample?

It could be a garbage collection issue.
0
 
sunithasAuthor Commented:
only thing is result set may return as many as 200,000 records

private int genrateXML(ResultSet rs, OutputStream os, String office_code,
                  String userID, int cia_id) {
            int counter = 0;
            try {

                  ReportDocument rd = ReportDocument.Factory.newInstance();
                  rd.addNewReport();
                  ReportDocument.Report report = rd.getReport();
                  report.setCiaID(cia_id);
                  report.setUserID(userID);
                  report.setGenerationTime(Calendar.getInstance());

                  while (rs.next()) {
                        ReportDocument.Report.ReferralActivity referalActivity = report
                        .addNewReferralActivity();
                        counter++;
                        if(counter >3)
                              break;
                  
                        referalActivity.setLastName(rs.getString("last_name"));
                        referalActivity.setState(rs.getString("State"));
                        referalActivity.setPostalCode(rs.getString("zip_code"));
                        referalActivity.setSocialSecurityNumber(rs.getString("ssn"));
                        referalActivity.setAccountNumber(rs
                                    .getString("DISP_CNSUMR_ACCT_NUM"));
                        if (rs.getDate("date_processed") != null)
                              referalActivity.setDateSent(rs.getDate("date_processed")
                                          .toString());
                        else
                              referalActivity.setDateSent("");
                        String temp;
                        if (rs.getString("status").equals("2"))
                              referalActivity.setPickedYN("Y");
                        else if (rs.getString("status").equals("1"))
                              referalActivity.setPickedYN("N");
                        
                        System.out.println(referalActivity.toString());
                  }
                  rd.save(os);
                  

            } catch (IOException e1) {

                  e1.printStackTrace();

            } catch (SQLException e) {

                  e.printStackTrace();
            }
            return counter;
      }
0
 
MogalManicCommented:
Try splitting the function into two parts
  1) Process Result set
  2) Save results to OutputStream

This will allow the garbage collector to clean up the result set before saving the results to the stream.  Currently there are three objects in the memory that are competing for space
 1) The Result set
 2) The bean
 3) The outputStream

Something like this:
      private int genrateXML(ResultSet rs, OutputStream os, String office_code,
                           String userID, int cia_id)
               {
        int[] counter = {0};
         try {
            ReportDocument rd=getReportDocument(rs, office_code, userID, cia_id, counter);
            rs.close();
            rd.save(os);
                 } catch (IOException e1) {
                    e1.printStackTrace();
                 } catch (SQLException e) {
                    e.printStackTrace();
                 }
                 return counter[0];
             }

      private ReportDocument getReportDocument(ResultSet rs, String office_code,String userID, int cia_id, int[] counter)
            throws SQLException
      {
            ReportDocument rd = ReportDocument.Factory.newInstance();
            rd.addNewReport();
            ReportDocument.Report report = rd.getReport();
            report.setCiaID(cia_id);
            report.setUserID(userID);
            report.setGenerationTime(Calendar.getInstance());

            while (rs.next()) {
                   ReportDocument.Report.ReferralActivity referalActivity = report
                   .addNewReferralActivity();
                   counter[0]++;
                   if(counter[0] >3)
                          break;
               
                   referalActivity.setLastName(rs.getString("last_name"));
                   referalActivity.setState(rs.getString("State"));
                   referalActivity.setPostalCode(rs.getString("zip_code"));
                   referalActivity.setSocialSecurityNumber(rs.getString("ssn"));
                   referalActivity.setAccountNumber(rs
                                 .getString("DISP_CNSUMR_ACCT_NUM"));
                   if (rs.getDate("date_processed") != null)
                          referalActivity.setDateSent(rs.getDate("date_processed")
                                          .toString());
                   else
                          referalActivity.setDateSent("");
                   String temp;
                   if (rs.getString("status").equals("2"))
                          referalActivity.setPickedYN("Y");
                   else if (rs.getString("status").equals("1"))
                          referalActivity.setPickedYN("N");
                   
                   System.out.println(referalActivity.toString());
            }
            return rd;
      }


Idealy you should close the resultset, statement and connection before saving the results to the outputstream
0
 
sunithasAuthor Commented:
hai ,


            I found the solution for my question.....  what i am doing is instead of creating a new node for every row(there are about 50,000 rows) what i am doing is create only one row and write it to outputstream and add keep doing the same....

this way there is only one node in the memory and my prob is solved


0
 
moduloCommented:
PAQed with points refunded (250)

modulo
Community Support Moderator
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now