jdk/test/sun/awt/image/ImageWatched/AddNoLeak.java
author dbuck
Wed, 21 Mar 2012 18:41:42 -0700
changeset 12388 5a31b31ab09b
permissions -rw-r--r--
7104147: the fix for cr6887286 was not appropriate for backporting Summary: Memory leak in the list of ImageObservers watching an object. We use recursion to iterate over the list. So once the list gets too big, we overflow the stack. Fix is to execute a prune operation on ever add, so there is no way we can get indefinite growth of stale nodes. Reviewed-by: prr, flar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12388
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
     1
/*
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
     4
 *
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
     7
 * published by the Free Software Foundation.
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
     8
 *
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    13
 * accompanied this code).
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    14
 *
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    18
 *
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    21
 * questions.
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    22
 */
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    23
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    24
import java.awt.image.ImageObserver;
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    25
import java.awt.image.ImageProducer;
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    26
import java.awt.image.ImageConsumer;
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    27
import java.awt.Image;
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    28
import java.awt.Container;
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    29
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    30
/* @test 1.0 12/01/17
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    31
   @bug 7104151
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    32
   @summary Make sure that we don't leak image observers (or related objects)
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    33
   @run main/othervm AddNoLeak
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    34
   @author David Buck
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    35
*/
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    36
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    37
public class AddNoLeak {
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    38
    public static void main(String[] args) {
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    39
        System.setProperty("java.awt.headless", "true");
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    40
        Container cont = new Container();
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    41
        Image img = cont.createImage(new DummyImageSource());
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    42
        for(int i=0;i < 15000;i++) {
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    43
            img.getWidth(new ImageObserver() {
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    44
                public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {return false;}
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    45
            });
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    46
            if (i % 100 == 0) {
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    47
                System.gc();
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    48
            }
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    49
        }
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    50
    }
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    51
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    52
    private static class DummyImageSource implements ImageProducer {
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    53
        public void addConsumer(ImageConsumer ic){}
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    54
        public boolean isConsumer(ImageConsumer ic){return false;}
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    55
        public void removeConsumer(ImageConsumer ic){}
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    56
        public void startProduction(ImageConsumer ic){}
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    57
        public void requestTopDownLeftRightResend(ImageConsumer ic){}
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    58
    }
5a31b31ab09b 7104147: the fix for cr6887286 was not appropriate for backporting
dbuck
parents:
diff changeset
    59
}