tags:

views:

74

answers:

1

I've asked the question the other day and rewritten the code to reflect the solutions, but now I have a new problem. Here is the code.

This is the calling/writing function; I have 8 types of proizvod which makes 8 XML files. After an XML file is created, it needs to be zipped by a custom zip class:

generateXML(tmpParam,queryRBR,proizvod.getOznaka());
writeToZip(proizvod.getOznaka());

This is inside writeToZip:

ZipEntry ze = new ZipEntry(oznaka + ".xml");
FileOutputStream fos = new FileOutputStream(new File(zipFolder + oznaka + ".zip"));
ZipOutputStream zos = new ZipOutputStream(fos);
zos.putNextEntry(ze);
FileInputStream fis = new FileInputStream(new File(zipFolder + oznaka + ".xml"));
final byte[] buffer = new byte[1024];
int n;
while ((n = fis.read(buffer)) != -1)
    zos.write(buffer, 0, n);
zos.closeEntry();
zos.flush();
zos.close();
fis.close();

This is inside generateXML:

PrintWriter writer = new PrintWriter(new BufferedOutputStream(new FileOutputStream(zipFolder +oznaka + ".xml")));
writer.print("\n<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
writer.print("\n<PROSTORNE_JEDINICE>");
stmt = cm.getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
String q = "";
rs = stmt.executeQuery(q);
if (rs != null) {
    System.out.println("Početak u : " + Util.nowTime());
    while (rs.next()) {
        writer.print("\n\t<row>");
        writer.print("\n\t\t<ID>" + Util.transformToHTML(rs.getInt("id")) + "</ID>");
        writer.print("\n\t\t<JED_ID>" + Util.transformToHTML(rs.getInt("jed_id")) + "</JED_ID>");
        //etc
        writer.print("\n\t</row>");
    }
    System.out.println("Kraj u : " + Util.nowTime());
}
writer.print("\n</PROSTORNE_JEDINICE>");

generateXML part still takes a lot of memory (if I'm guessing correctly, it takes bit by bit as much as it can) and I don't see how I could optimize it (use an alternative way to feed the writer.print function)?

A: 

two things I see instantly (maybe copy/paste-error) are, that you don't close the PrintWriter (writer) in generateXml and you don't close FileOutputStream (fos) in WriteToZipFile.

To find Memory-gliches I'd recommend dotTrace - maybe you can find some irregularities in your code-behaviour.

Gambrinus
The link you are providing to dotTrace links to a profiler for .NET, which doesn't seem like it would be too useful for a Java app.
Jim Kiley
Writer is closed later in the code but it is "irrelevant"; i've commented the code so the method only runs once (for the largest "proizvod") so I don't think that affects it. Only 2 things come to mind, first is that strings are being kept in memory, and second, that while writing to file, the file is being kept in memory. I guess I could try to open then close it every iteration?
Andrija
@Gambrinus: Also, don't you have to close the RecordSet object?@Andrija: In generateXML, you could try flushing the PrintWriter, maybe after each row is written.
Michael Angstadt