如何从内存中删除元素

时间:2015-05-25 14:05:49

标签: java element

我有发送屏幕循环,当内存达到2 GB程序停止时。 我如何从内存中删除已发送的图像?

var delegate: MainToolbarDelegate = self.view.window.toolbar

1 个答案:

答案 0 :(得分:0)

所有运行功能:

        public void run() {
        java.awt.image.BufferedImage img = null;
        Robot r = null;
        try {
            r = new Robot();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle rect = new Rectangle(0, 0, size.width, size.height);
        javax.swing.ImageIcon icon = null;

        while (true) {
            try {
                System.gc();
                img = r.createScreenCapture(rect);
                icon = new javax.swing.ImageIcon(img);
                os.writeObject(icon);
                os.flush();
                icon = null; // coś nie działa usuwanie bo pamięć rośnie
                System.gc();
                try {
                    Thread.currentThread();
                    Thread.sleep(sleepInterval);
                } catch (Exception e) {
                }
            } catch (Exception ex) {
                closeAll();
                break;
            }
        }
    }