白季飞龙的个人主页

Java垃圾回收大杂烩

测试GC

package bj;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.FontUIResource;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

/**
 * Created by BaiJiFeiLong@gmail.com at 18-9-8 下午1:16
 */
public class Foo {
    private static JLabel label;

    public static void main(String[] args) {
        @SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
        List<Pet> pets = new ArrayList<>();
        Stream.of("Button", "Label")
                .map($ -> $.concat(".font"))
                .forEach($ -> UIManager.put($, new FontUIResource("Consolas", Font.PLAIN, 44)));
        JFrame frame = new JFrame("Hello World");
        frame.setSize(600, 400);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setContentPane(new Box(BoxLayout.Y_AXIS) {{
            setBorder(new EmptyBorder(20, 20, 20, 20));
            add(new JLabel("Ready") {{
                label = this;
                setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
            }});
            add(new JButton("Click Me") {{
                updateLabel(pets.size());
                setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
                addActionListener(e -> {
                    pets.add(new Pet());
                    updateLabel(pets.size());
                });
            }});
        }});
        frame.setVisible(true);
    }

    private static void updateLabel(int i) {
        label.setText("Pets size: " + i);
    }
}

class Pet {
    @SuppressWarnings("unused")
    private byte[] garbage = new byte[1024 * 1024];
}

漫漫路,莫论逍遥;潜心修,只为悟道