I am not sure what is going on. Perhaps this Runtime reporting wrong values.

I see that number printed to the console is growing but at the same time i do not observe heap growth in the jconsole.

-igor


[EMAIL PROTECTED] wrote:
As requested, here a small runnable test. When you run it please start the JVM with 
something like "-XX:NewSize=256m -XX:MaxNewSize=256m", elsehow the memory 
message wont be of much use.

Here comes the code:

-----------------------------------------------------------------------------------------------------------------------

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.awt.image.MemoryImageSource;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * Small example to compare MemoryImageSource and BufferedImage.
* * @author kif
 *
 */
public class SetPixelTest {
        private static MyDrawingArea _mda;
        private static boolean _use_mis = false;
        private static JCheckBox _jcb_use_mis;
        
        public static class MyDrawingArea extends JPanel {
                private static int IMG_WIDTH = 256;
                private static int IMG_HEIGHT = 256;
                
                // either use a BufferedImage ...
                private BufferedImage _bimg;
                
                // ... or a MemoryImageSource
                private Image _img;
                protected int[] _pixels;
                protected MemoryImageSource _mis;
                
                public MyDrawingArea() {
                        setPreferredSize(new Dimension(400,400));
                        setBackground(Color.black);
                        
                        _bimg = new BufferedImage(IMG_WIDTH,IMG_HEIGHT, 
BufferedImage.TYPE_INT_ARGB);
                        _pixels = new int[IMG_WIDTH*IMG_HEIGHT];
                        _mis = new MemoryImageSource(IMG_WIDTH, IMG_HEIGHT, 
_pixels, 0, IMG_WIDTH);
                        _mis.setAnimated(true);
                        _img = createImage(_mis);
                }
                
                public void random_fill() {
                        Random rand = new Random();
                        int r, g, b, rgb;
                        
                        if (_use_mis) { // MemoryImage Source
                                int pixel_counter = 0;
                                for (int y=0; y<IMG_HEIGHT; y++) {
                                        for (int x=0; x<IMG_WIDTH; x++) {
                                                r = rand.nextInt() % 256;
                                                g = rand.nextInt() % 256;
                                                b = rand.nextInt() % 256;
                                                rgb = 0xff<<24 | r<<16 | g<<8 | 
b;
                                                _pixels[pixel_counter++] = rgb;
                                        }
                                }
                                _mis.newPixels();
                        } else { // BufferedImage
                                for (int y=0; y<IMG_HEIGHT; y++) {
                                        for (int x=0; x<IMG_WIDTH; x++) {
                                                r = rand.nextInt() % 256;
                                                g = rand.nextInt() % 256;
                                                b = rand.nextInt() % 256;
                                                rgb = 0xff<<24 | r<<16 | g<<8 | 
b;

                                                _bimg.setRGB(x, y, rgb);
                                        }
                                }
                        }
                        repaint();
                }
                
                public void paint(Graphics g) {
                        if (_use_mis) { // MemoryImage Source
                                g.drawImage(_img, 0, 0, getWidth(), 
getHeight(), this);
                        } else { // BufferedImage
                                g.drawImage(_bimg, 0, 0, getWidth(), 
getHeight(), this);
                        }
                }
        }
        
        /**
         * @param args
         */
        public static void main(String[] args) {
                JFrame window = new JFrame("Pixel Performance Test");
                _mda = new MyDrawingArea();
                
                // if someone closes the main window, the program shall end.
                window.addWindowListener(new WindowAdapter() {
                                public void windowClosing(WindowEvent e) {
                                        System.exit(0);
                                }
                });

                JButton jb_fill = new JButton("Random Fill");
                jb_fill.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                for (int i=0; i<10; i++) {
                                        _mda.random_fill();
                                }
                                
                                
System.out.println((Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())/(1024*1024)
                                +" / 
"+Runtime.getRuntime().totalMemory()/(1024*1024)+"MB");
                        }
                });

                _jcb_use_mis = new JCheckBox("Use MemoryImageSource");
                _jcb_use_mis.addItemListener(new ItemListener() {
                        public void itemStateChanged(ItemEvent e) {
                                _use_mis = _jcb_use_mis.isSelected();
                        }
                });
                JPanel buttons = new JPanel();
                buttons.add(jb_fill);
                buttons.add(_jcb_use_mis);

                window.setLayout(new BorderLayout());
                window.add(_mda, BorderLayout.CENTER);
                window.add(buttons, BorderLayout.SOUTH);
                window.pack();
                
                window.setVisible(true);
        }

}

-----------------------------------------------------------------------------------------------------------------------

Just run it and press the button a few times. Then click on the Checkbox and 
repeat.

MfG;
ChaosE
[Message sent by forum member 'chaose71' (chaose71)]

http://forums.java.net/jive/thread.jspa?messageID=269278

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to