Hello all,
in attachment you can find a standalone example with a series
of tests. This code will only work with the iText version in
the SVN repository.
Six PDF files are generated:
2 with Chapters/Sections
2 with a PdfPTable
2 with a Tabe
Of each pair, one was generated 'the traditional way';
the other using the new memory management functionality
introduced with the LargeElement interface.
The output of both methods should be indentical, but
the difference in memory usage is huge!
Note that this functionality probably needs more testing.
br,
Bruno
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import com.lowagie.text.Chapter;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Section;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public class MemoryTests {

        private boolean test;
        private long memory_use;
        private long initial_memory_use = 0l;
        private long maximum_memory_use = 0l;
        OutputStreamWriter writer = new OutputStreamWriter(System.out);
        
        public static void main(String[] args) {
                MemoryTests tests = new MemoryTests();
                tests.createPdfs();
        }
        
        public void createPdfs() {
                try {
                        resetMaximum();
                        test = false;
                        
writer.write("test_chapters_without_memory_management.pdf");
                        
createPdfWithChapters("test_chapters_without_memory_management.pdf");
                        resetMaximum();
                        test = true;
                        
writer.write("test_chapters_with_memory_management.pdf");
                        
createPdfWithChapters("test_chapters_with_memory_management.pdf");
                        resetMaximum();
                        test = false;
                        
writer.write("test_pdfptable_without_memory_management.pdf");
                        
createPdfWithPdfPTable("test_pdfptable_without_memory_management.pdf");
                        resetMaximum();
                        test = true;
                        
writer.write("test_pdfptable_with_memory_management.pdf");
                        
createPdfWithPdfPTable("test_pdfptable_with_memory_management.pdf");
                        resetMaximum();
                        test = false;
                        
writer.write("test_table_without_memory_management.pdf");
                        
createPdfWithTable("test_table_without_memory_management.pdf");
                        resetMaximum();
                        test = true;
                        writer.write("test_table_with_memory_management.pdf");
                        
createPdfWithTable("test_table_with_memory_management.pdf");
                        resetMaximum();
                        writer.flush();
                        writer.close();
                }
                catch(Exception e) {
                        e.printStackTrace();
                }
        }

        private void createPdfWithChapters(String filename) throws 
FileNotFoundException, DocumentException {
                // step 1: creation of a document-object
                Document document = new Document();
                // step 2: we create a writer
                PdfWriter.getInstance(
                                document,
                                new FileOutputStream(filename));
                // step 3: we open the document
                document.open();
                // step 4: we add content to the document
                Chapter chapter;
                Section section;
                Section subsection;
                chapter = new Chapter(new Paragraph("chapter", new 
Font(Font.HELVETICA, 14, Font.BOLD)), 1);
                if(test) chapter.setComplete(false);
                addContent(chapter, 0);
                if (test) document.add(chapter);
                checkpoint();
                section = chapter.addSection("section 1", 1);
                if(test) section.setComplete(false);
                addContent(section, 1);
                if (test) document.add(chapter);
                checkpoint();
                subsection = section.addSection("subsection 1", 2);
                addContent(subsection, 2);
                if (test) document.add(chapter);
                checkpoint();
                subsection = section.addSection("subsection 2", 2);
                addContent(subsection, 3);
                if (test) document.add(chapter);
                checkpoint();
                subsection = section.addSection("subsection 3", 2);
                addContent(subsection, 4);
                if (test) document.add(chapter);
                checkpoint();
                section = chapter.addSection("section 2", 1);
                if(test) section.setComplete(false);
                subsection = section.addSection("subsection 1", 2);
                addContent(subsection, 5);
                if (test) document.add(chapter);
                checkpoint();
                subsection = section.addSection("subsection 2", 2);
                addContent(subsection, 6);
                if (test) chapter.setComplete(true);
                document.add(chapter);
                checkpoint();
                // step 5: we close the document
                document.close();
        }

        private void createPdfWithTable(String filename) throws 
FileNotFoundException, DocumentException {
                // step 1
                Document document = new Document();
                // step 2
                PdfWriter.getInstance(document, new FileOutputStream(filename));
                // step 3: we open the document
                document.open();
                // step 4
                Table table = new Table(2);
                table.addCell("Header1");
                table.addCell("Header2");
                table.endHeaders();
                if (test) table.setComplete(false);
                for (int i = 0; i < 500; i++) {
                        if (i % 50 == 50 - 1) {
                                if (test) document.add(table);
                                checkpoint();
                        }
                        table.addCell(String.valueOf(i));
                        table.addCell("Quick brown fox jumps over the lazy 
dog.");
                }
                if (test) table.setComplete(true);
                document.add(table);
                checkpoint();
                // step 5
                document.close();
        }

        private void createPdfWithPdfPTable(String filename) throws 
FileNotFoundException, DocumentException {
                // step 1
                Document document = new Document();
                // step 2
                PdfWriter.getInstance(document, new FileOutputStream(filename));
                // step 3: we open the document
                document.open();
                // step 4
                PdfPTable table = new PdfPTable(2);
                table.setHeaderRows(1);
                table.addCell("Header1");
                table.addCell("Header2");
                if (test) table.setComplete(false);
                for (int i= 1; i <= 500; i++) {
                        if (i % 50 == 50 - 1) {
                                if (test) document.add(table);
                                checkpoint();
                        }
                        table.addCell(String.valueOf(i));
                        table.addCell("Quick brown fox jumps over the lazy 
dog.");
                }
                if (test) table.setComplete(true);
                document.add(table);
                checkpoint();
                // step 5
                document.close();
        }
        
        private void addContent(Section section, int s) {
                for (int i = 1; i < 100; i++) {
                        StringBuffer buf = new StringBuffer("(");
                        buf.append(i);
                        buf.append(") ");
                        buf.append(BART_SIMPSON[s]);
                        section.add(new Paragraph(buf.toString()));
                }
        }
        
        private void checkpoint() throws DocumentException {
                memory_use = getMemoryUse();
                maximum_memory_use = Math.max(maximum_memory_use, memory_use);
                println("memory use: ", memory_use);
        }
        
        private void resetMaximum() {
                println("maximum: ", maximum_memory_use);
                println("total used: ", maximum_memory_use - 
initial_memory_use);
                maximum_memory_use = 0l;
                initial_memory_use = getMemoryUse();
                println("initial memory use: ", initial_memory_use);
        }
        
        private void println(String message, long l) {
                try {
                        writer.write(message + l);
                        writer.write("\n");
                        writer.flush();
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }
        
        /**
         * Returns the current memory use.
         * 
         * @return the current memory use
         */
        private static long getMemoryUse() {
                garbageCollect();
                garbageCollect();
                long totalMemory = Runtime.getRuntime().totalMemory();
                garbageCollect();
                garbageCollect();
                long freeMemory = Runtime.getRuntime().freeMemory();
                return (totalMemory - freeMemory);
        }
        
        /**
         * Makes sure all garbage is cleared from the memory.
         */
        private static void garbageCollect() {
                try {
                        System.gc();
                        Thread.sleep(100);
                        System.runFinalization();
                        Thread.sleep(100);
                        System.gc();
                        Thread.sleep(100);
                        System.runFinalization();
                        Thread.sleep(100);
                } catch (InterruptedException ex) {
                        ex.printStackTrace();
                }
        }
        public static final String[] BART_SIMPSON =
        {
                "I will not bribe Principal Skinner.",
                "\"Bart Bucks\" are not legal tender.",
                "There are plenty of businesses like show business.",
                "I did not see Elvis.",
                "A burp is not an answer.",
                "I will not celebrate meaningless milestones.",
                "I will not go very far with this attitude."
        };
}

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature

-------------------------------------------------------------------------
SF.Net email is sponsored by: The Future of Linux Business White Paper
from Novell.  From the desktop to the data center, Linux is going
mainstream.  Let it simplify your IT future.
http://altfarm.mediaplex.com/ad/ck/8857-50307-18918-4
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/

Reply via email to