jdk/src/share/demo/management/MemoryMonitor/MemoryMonitor.java
author jgish
Thu, 18 Apr 2013 16:33:11 -0400
changeset 17159 bb566a21b661
parent 14342 8435a30053c1
permissions -rw-r--r--
8012005: LogManager needs test to ensure stack trace is not being done to find bundle Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 12866
diff changeset
     2
 * Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * are met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/*
10292
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    33
 * This source code is provided to illustrate the usage of a given feature
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    34
 * or technique and has been deliberately simplified. Additional steps
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    35
 * required for a production-quality application, such as security checks,
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    36
 * input validation and proper error handling, might not be present in
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    37
 * this sample code.
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    38
 */
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    39
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    40
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9520
diff changeset
    41
/*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.awt.image.BufferedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.awt.geom.Line2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.awt.geom.Rectangle2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import javax.swing.border.EtchedBorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import javax.swing.border.TitledBorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.lang.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * Demo code which plots the memory usage by all memory pools.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * The memory usage is sampled at some time interval using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * java.lang.management API. This demo code is modified based
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * java2d MemoryMonitor demo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
public class MemoryMonitor extends JPanel {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
    62
    private static final long serialVersionUID = -3463003810776195761L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    static JCheckBox dateStampCB = new JCheckBox("Output Date Stamp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public Surface surf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    JPanel controls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    boolean doControls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    JTextField tf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // Get memory pools.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    static java.util.List<MemoryPoolMXBean> mpools =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        ManagementFactory.getMemoryPoolMXBeans();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // Total number of memory pools.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    static int numPools = mpools.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public MemoryMonitor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        setLayout(new BorderLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        setBorder(new TitledBorder(new EtchedBorder(), "Memory Monitor"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        add(surf = new Surface());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        controls = new JPanel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        controls.setPreferredSize(new Dimension(135,80));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        Font font = new Font("serif", Font.PLAIN, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        JLabel label = new JLabel("Sample Rate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        label.setFont(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        label.setForeground(Color.red);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        controls.add(label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        tf = new JTextField("1000");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        tf.setPreferredSize(new Dimension(45,20));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        controls.add(tf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        controls.add(label = new JLabel("ms"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        label.setFont(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        label.setForeground(Color.red);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        controls.add(dateStampCB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        dateStampCB.setFont(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        addMouseListener(new MouseAdapter() {
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
    94
            @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            public void mouseClicked(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
               removeAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
               if ((doControls = !doControls)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                   surf.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                   add(controls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
               } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                   try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                       surf.sleepAmount = Long.parseLong(tf.getText().trim());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                   } catch (Exception ex) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                   surf.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                   add(surf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
               validate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
               repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public class Surface extends JPanel implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        public Thread thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        public long sleepAmount = 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        public int  usageHistCount = 20000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        private int w, h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        private BufferedImage bimg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        private Graphics2D big;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        private Font font = new Font("Times New Roman", Font.PLAIN, 11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        private int columnInc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        private float usedMem[][];
12866
48fd3e04f1f6 7173044: MemoryMonitor hangs if getMax method in MemoryUsage object returns -1
zhouyx
parents: 11841
diff changeset
   125
        private float usedMemMax[]; // Used when max pool size is undefined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        private int ptNum[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        private int ascent, descent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        private Rectangle graphOutlineRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        private Rectangle2D mfRect = new Rectangle2D.Float();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        private Rectangle2D muRect = new Rectangle2D.Float();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        private Line2D graphLine = new Line2D.Float();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        private Color graphColor = new Color(46, 139, 87);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        private Color mfColor = new Color(0, 100, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        private String usedStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        public Surface() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            setBackground(Color.black);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            addMouseListener(new MouseAdapter() {
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   140
                @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                public void mouseClicked(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    if (thread == null) start(); else stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            usedMem = new float[numPools][];
12866
48fd3e04f1f6 7173044: MemoryMonitor hangs if getMax method in MemoryUsage object returns -1
zhouyx
parents: 11841
diff changeset
   146
            usedMemMax = new float[numPools];
48fd3e04f1f6 7173044: MemoryMonitor hangs if getMax method in MemoryUsage object returns -1
zhouyx
parents: 11841
diff changeset
   147
            for (int i = 0; i < numPools; i++) {
48fd3e04f1f6 7173044: MemoryMonitor hangs if getMax method in MemoryUsage object returns -1
zhouyx
parents: 11841
diff changeset
   148
                usedMemMax[i] = 1024f * 1024f ;
48fd3e04f1f6 7173044: MemoryMonitor hangs if getMax method in MemoryUsage object returns -1
zhouyx
parents: 11841
diff changeset
   149
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            ptNum = new int[numPools];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   153
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        public Dimension getMinimumSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            return getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   158
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        public Dimension getMaximumSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            return getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   163
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        public Dimension getPreferredSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            return new Dimension(135,80);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   169
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        public void paint(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            if (big == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            big.setBackground(getBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            big.clearRect(0,0,w,h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            h = h / ((numPools + numPools%2) / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            w = w / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            int k=0; // index of memory pool.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            for (int i=0; i < 2;i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
               for (int j=0; j < (numPools + numPools%2)/ 2; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                 plotMemoryUsage(w*i,h*j,w,h,k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                 if (++k >= numPools) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    i = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    j = (numPools + numPools%2)/ 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            g.drawImage(bimg, 0, 0, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        public void plotMemoryUsage(int x1, int y1, int x2, int y2, int npool) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            MemoryPoolMXBean mp = mpools.get(npool);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            float usedMemory =  mp.getUsage().getUsed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            float totalMemory =  mp.getUsage().getMax();
12866
48fd3e04f1f6 7173044: MemoryMonitor hangs if getMax method in MemoryUsage object returns -1
zhouyx
parents: 11841
diff changeset
   202
            if (totalMemory < 0) { // Max is undefined for this pool
48fd3e04f1f6 7173044: MemoryMonitor hangs if getMax method in MemoryUsage object returns -1
zhouyx
parents: 11841
diff changeset
   203
                if (usedMemory > usedMemMax[npool]) {
48fd3e04f1f6 7173044: MemoryMonitor hangs if getMax method in MemoryUsage object returns -1
zhouyx
parents: 11841
diff changeset
   204
                    usedMemMax[npool] = usedMemory;
48fd3e04f1f6 7173044: MemoryMonitor hangs if getMax method in MemoryUsage object returns -1
zhouyx
parents: 11841
diff changeset
   205
                }
48fd3e04f1f6 7173044: MemoryMonitor hangs if getMax method in MemoryUsage object returns -1
zhouyx
parents: 11841
diff changeset
   206
                totalMemory = usedMemMax[npool];
48fd3e04f1f6 7173044: MemoryMonitor hangs if getMax method in MemoryUsage object returns -1
zhouyx
parents: 11841
diff changeset
   207
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            // .. Draw allocated and used strings ..
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            big.setColor(Color.green);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            // Print Max memory allocated for this memory pool.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            big.drawString(String.valueOf((int)totalMemory/1024) + "K Max ", x1+4.0f, (float) y1 + ascent+0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            big.setColor(Color.yellow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            // Print the memory pool name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            big.drawString(mp.getName(),  x1+x2/2, (float) y1 + ascent+0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            // Print the memory used by this memory pool.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            usedStr = String.valueOf((int)usedMemory/1024)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                + "K used";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            big.setColor(Color.green);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            big.drawString(usedStr, x1+4, y1+y2-descent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            // Calculate remaining size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            float ssH = ascent + descent;
11841
38a39c748880 7143230: fix warnings in java.util.jar, sun.tools.jar, zipfs demo, etc.
smarks
parents: 10292
diff changeset
   227
            float remainingHeight = y2 - (ssH*2) - 0.5f;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            float blockHeight = remainingHeight/10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            float blockWidth = 20.0f;
11841
38a39c748880 7143230: fix warnings in java.util.jar, sun.tools.jar, zipfs demo, etc.
smarks
parents: 10292
diff changeset
   230
            float remainingWidth = x2 - blockWidth - 10;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            // .. Memory Free ..
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            big.setColor(mfColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            int MemUsage = (int) (((totalMemory - usedMemory) / totalMemory) * 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            for ( ; i < MemUsage ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                mfRect.setRect(x1+5,(float) y1+ssH+i*blockHeight,
11841
38a39c748880 7143230: fix warnings in java.util.jar, sun.tools.jar, zipfs demo, etc.
smarks
parents: 10292
diff changeset
   238
                                blockWidth, blockHeight-1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                big.fill(mfRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            // .. Memory Used ..
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            big.setColor(Color.green);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            for ( ; i < 10; i++)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                muRect.setRect(x1+5,(float) y1 + ssH+i*blockHeight,
11841
38a39c748880 7143230: fix warnings in java.util.jar, sun.tools.jar, zipfs demo, etc.
smarks
parents: 10292
diff changeset
   246
                                blockWidth, blockHeight-1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                big.fill(muRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            // .. Draw History Graph ..
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            if (remainingWidth <= 30) remainingWidth = (float)30;
11841
38a39c748880 7143230: fix warnings in java.util.jar, sun.tools.jar, zipfs demo, etc.
smarks
parents: 10292
diff changeset
   252
            if (remainingHeight <= ssH) remainingHeight = ssH;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            big.setColor(graphColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            int graphX = x1+30;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            int graphY = y1 + (int) ssH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            int graphW = (int) remainingWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            int graphH = (int) remainingHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            graphOutlineRect.setRect(graphX, graphY, graphW, graphH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            big.draw(graphOutlineRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            int graphRow = graphH/10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            // .. Draw row ..
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            for (int j = graphY; j <= graphH+graphY; j += graphRow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                graphLine.setLine(graphX,j,graphX+graphW,j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                big.draw(graphLine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            // .. Draw animated column movement ..
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            int graphColumn = graphW/15;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            if (columnInc == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                columnInc = graphColumn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            for (int j = graphX+columnInc; j < graphW+graphX; j+=graphColumn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                graphLine.setLine(j,graphY,j,graphY+graphH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                big.draw(graphLine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            --columnInc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            // Plot memory usage by this memory pool.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            if (usedMem[npool] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                usedMem[npool] = new float[usageHistCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                ptNum[npool] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            // save memory usage history.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            usedMem[npool][ptNum[npool]] = usedMemory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            big.setColor(Color.yellow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            int w1; // width of memory usage history.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            if (ptNum[npool] > graphW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                w1 = graphW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                w1 = ptNum[npool];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            for (int j=graphX+graphW-w1, k=ptNum[npool]-w1; k < ptNum[npool];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                                                                k++, j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                 if (k != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                     if (usedMem[npool][k] != usedMem[npool][k-1]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                         int h1 = (int)(graphY + graphH * ((totalMemory -usedMem[npool][k-1])/totalMemory));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                         int h2 = (int)(graphY + graphH * ((totalMemory -usedMem[npool][k])/totalMemory));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                         big.drawLine(j-1, h1, j, h2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                     } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                         int h1 = (int)(graphY + graphH * ((totalMemory -usedMem[npool][k])/totalMemory));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                         big.fillRect(j, h1, 1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            if (ptNum[npool]+2 == usedMem[npool].length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                // throw out oldest point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                for (int j = 1;j < ptNum[npool]; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                     usedMem[npool][j-1] = usedMem[npool][j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                --ptNum[npool];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                ptNum[npool]++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        public void start() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            thread = new Thread(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            thread.setPriority(Thread.MIN_PRIORITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            thread.setName("MemoryMonitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            thread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        public synchronized void stop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            thread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   341
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            Thread me = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            while (thread == me && !isShowing() || getSize().width == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                try {
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   348
                    Thread.sleep(500);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                } catch (InterruptedException e) { return; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            while (thread == me && isShowing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                Dimension d = getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                if (d.width != w || d.height != h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    w = d.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    h = d.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    bimg = (BufferedImage) createImage(w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    big = bimg.createGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    big.setFont(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    FontMetrics fm = big.getFontMetrics(font);
11841
38a39c748880 7143230: fix warnings in java.util.jar, sun.tools.jar, zipfs demo, etc.
smarks
parents: 10292
diff changeset
   361
                    ascent = fm.getAscent();
38a39c748880 7143230: fix warnings in java.util.jar, sun.tools.jar, zipfs demo, etc.
smarks
parents: 10292
diff changeset
   362
                    descent = fm.getDescent();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                try {
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   366
                    Thread.sleep(sleepAmount);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                } catch (InterruptedException e) { break; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                if (MemoryMonitor.dateStampCB.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                     System.out.println(new Date().toString() + " " + usedStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            thread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    // Test thread to consume memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    static class Memeater extends ClassLoader implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        Object y[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        public Memeater() {}
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   381
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            y = new Object[10000000];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            int k =0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            while(true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                 if (k == 5000000) k=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                 y[k++] = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                 try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                     Thread.sleep(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                 } catch (Exception x){}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                 // to consume perm gen storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                 try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                     // the classes are small so we load 10 at a time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                     for (int i=0; i<10; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                        loadNext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                 } catch (ClassNotFoundException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                   // ignore exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   406
        Class<?> loadNext() throws ClassNotFoundException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            // public class TestNNNNNN extends java.lang.Object{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            // public TestNNNNNN();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            //   Code:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            //    0:    aload_0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            //    1:    invokespecial   #1; //Method java/lang/Object."<init>":()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            //    4:    return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            // }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            int begin[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                0xca, 0xfe, 0xba, 0xbe, 0x00, 0x00, 0x00, 0x30,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                0x00, 0x0a, 0x0a, 0x00, 0x03, 0x00, 0x07, 0x07,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                0x00, 0x08, 0x07, 0x00, 0x09, 0x01, 0x00, 0x06,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                0x3c, 0x69, 0x6e, 0x69, 0x74, 0x3e, 0x01, 0x00,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                0x03, 0x28, 0x29, 0x56, 0x01, 0x00, 0x04, 0x43,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                0x6f, 0x64, 0x65, 0x0c, 0x00, 0x04, 0x00, 0x05,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                0x01, 0x00, 0x0a, 0x54, 0x65, 0x73, 0x74 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            int end [] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                0x01, 0x00, 0x10,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                0x6a, 0x61, 0x76, 0x61, 0x2f, 0x6c, 0x61, 0x6e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                0x67, 0x2f, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                0x00, 0x21, 0x00, 0x02, 0x00, 0x03, 0x00, 0x00,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x04,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                0x00, 0x05, 0x00, 0x01, 0x00, 0x06, 0x00, 0x00,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                0x00, 0x11, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                0x00, 0x05, 0x2a, 0xb7, 0x00, 0x01, 0xb1, 0x00,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                0x00, 0x00, 0x00, 0x00, 0x00 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            // TestNNNNNN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            String name = "Test" + Integer.toString(count++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            byte value[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                value = name.substring(4).getBytes("UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            } catch (java.io.UnsupportedEncodingException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            // construct class file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            int len = begin.length + value.length + end.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            byte b[] = new byte[len];
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   452
            int pos=0;
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   453
            for (int i: begin) {
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   454
                b[pos++] = (byte) i;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            }
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   456
            for (byte v: value) {
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   457
                b[pos++] = v;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            }
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   459
            for (int e: end) {
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   460
                b[pos++] = (byte) e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            return defineClass(name, b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        static int count = 100000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    public static void main(String s[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        final MemoryMonitor demo = new MemoryMonitor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        WindowListener l = new WindowAdapter() {
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   473
            @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            public void windowClosing(WindowEvent e) {System.exit(0);}
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   475
            @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            public void windowDeiconified(WindowEvent e) { demo.surf.start(); }
9520
99d378796e54 7029383: Refresh of non-client demos
nloodin
parents: 5506
diff changeset
   477
            @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            public void windowIconified(WindowEvent e) { demo.surf.stop(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        JFrame f = new JFrame("MemoryMonitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        f.addWindowListener(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        f.getContentPane().add("Center", demo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        f.pack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        f.setSize(new Dimension(400,500));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        f.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        demo.surf.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        Thread thr = new Thread(new Memeater());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        thr.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
}