Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-7001133

OutOfMemoryError by CustomMediaSizeName implementation

    XMLWordPrintable

Details

    • 2d
    • b03
    • x86
    • linux

    Backports

      Description

        FULL PRODUCT VERSION :
        java version "1.6.0_22"
        Java(TM) SE Runtime Environment (build 1.6.0_22-b04)

        ADDITIONAL OS VERSION INFORMATION :
        Linux <user> 2.6.34.7-61.fc13.i686.PAE #1 SMP Tue Oct 19 04:24:06 UTC 2010 i686 i686 i386 GNU/Linux

        EXTRA RELEVANT SYSTEM CONFIGURATION :
        Cups version 1.4.4 with four printers installed

        VM arguments:
        -Xmx4m (reduce to get the OutOfMemoryError faster)
        -XX:-HeapDumpOnOutOfMemoryError

        A DESCRIPTION OF THE PROBLEM :
        There is a memory leak in class sun.print.CustomMediaSizeName that can be forced by using the PrintService.addPrintServiceAttributeListener().

        The constructor of class CustomMediaSizeName adds the name and itself into a static list. So with every creation the list gets bigger and bigger and will never be cleared.
        Using the PrintService.addPrintServiceAttributeListener() always creates a set of ~60 instances of this class that will be added to the list.

        Using a HashMap would solve this problem, because there is no need to have thousands of "A4" media size names in memory.

        Source code (http://www.docjar.com/html/api/sun/print/CustomMediaSizeName.java.html):

        class CustomMediaSizeName extends MediaSizeName {
            private static ArrayList customStringTable = new ArrayList();
            private static ArrayList customEnumTable = new ArrayList();
            private String choiceName;
            private MediaSizeName mediaName;

            private CustomMediaSizeName(int x) {
        super(x);

            }

            private synchronized static int nextValue(String name) {
              customStringTable.add(name);
              
              return (customStringTable.size()-1);
            }

            public CustomMediaSizeName(String name) {
        super(nextValue(name));
        customEnumTable.add(this);
        choiceName = null;
        mediaName = null;
            }
            ....
        }

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        see source code


        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        package de.comsoft.cadas.tools;

        import javax.print.DocFlavor;
        import javax.print.PrintService;
        import javax.print.PrintServiceLookup;
        import javax.print.event.PrintServiceAttributeEvent;
        import javax.print.event.PrintServiceAttributeListener;

        public class TestCustomMediaSizeName
                implements PrintServiceAttributeListener {

            public static void main(final String[] args) {
                final TestCustomMediaSizeName test = new TestCustomMediaSizeName();
                test.run();
            }

            private void run() {
                final PrintService[] services = PrintServiceLookup.lookupPrintServices(
                        DocFlavor.SERVICE_FORMATTED.PRINTABLE, null);
                for (final PrintService printService : services) {
                    printService.addPrintServiceAttributeListener(this);
                }

                try {
                    Thread.sleep(Long.MAX_VALUE);
                } catch (final InterruptedException ex) {
                    // TODO Auto-generated catch block
                    ex.printStackTrace();
                }
            }

            public void attributeUpdate(final PrintServiceAttributeEvent pPsae) {
                // nothing
            }
        }

        ---------- END SOURCE ----------

        Attachments

          Issue Links

            Activity

              People

                prr Philip Race
                webbuggrp Webbug Group
                Votes:
                0 Vote for this issue
                Watchers:
                7 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: