Android Studio - 旋转设备会导致使用越来越多的内存

时间:2016-06-11 23:02:58

标签: android android-studio memory rotation

我有这个简单的Android Studio项目,用于测试小代码片段。今天,当我摆弄一些代码时,我注意到每次旋转设备时,都会导致应用程序使用越来越多的内存。该图表的屏幕截图如下:

memory increase as I keep rotating the device

每次旋转设备时,分配的内存都会增加,可用内存量会减少。当空闲内存达到零时,它会清除一块内存,但不是全部。随着我不断地来回旋转设备,最终它会为应用程序提供更多可用内存,分配的内存不断增加和增加。

这是一个简单的应用程序,只有一个TextView,根本没有执行代码。没有数据库活动,没有网络活动,没有游标,没有什么可以导致任何内存泄漏。

以下是MainActivity.java内容:

private JLabel messageLabel;
private JButton playlist;
private JPanel panel;
BufferedImage image;
AudioStream audioStream1, audioStream2, audioStream3;
//Object[] music = new Object[3];
private final int WINDOW_WIDTH = 800;
private final int WINDOW_HEIGHT = 525;

// File destinationss    
private String s1 = "C:\\Users\\Tony\\Desktop\\Java\\NetBeansProjects\\Gui Stuff\\src\\No_Pressure.wav";
private String s2 = "C:\\Users\\Tony\\Desktop\\Java\\NetBeansProjects\\Gui Stuff\\src\\Grateful_Dead_-_Touch_of_Grey.wav";
private String s3 = "C:\\Users\\Tony\\Desktop\\Java\\NetBeansProjects\\Gui Stuff\\src\\Stairway_to_Heaven_Led_Zeppelin_Lyrics.wav";

InputStream in1 = new FileInputStream(s1);
InputStream in2 = new FileInputStream(s2);
InputStream in3 = new FileInputStream(s3);
private ArrayList music;

public JukeBoxWithArrays() throws IOException {

    music = new ArrayList();

    audioStream1 = new AudioStream(in1);
    audioStream2 = new AudioStream(in2);
    audioStream3 = new AudioStream(in3);

    music.add(audioStream1);
    music.add(audioStream2);
    music.add(audioStream3);

    setTitle("Juke Box Playlist");
    setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    messageLabel = new JLabel("Click the Button to play the playlist");

    // Create the Playlist button
    playlist = new JButton("Playlist number 1");

    // Register the event Listener
    playlist.addActionListener(new PlaylistListener());

    // Create the panel
    panel = new JPanel();
    image = ImageIO.read(new File("C:\\Users\\Tony\\Desktop\\Java\\NetBeansProjects\\Gui Stuff\\src\\jukebox2.jpg"));
    panel.add(messageLabel);
    panel.add(playlist);
    panel.add((new JLabel(new ImageIcon(image))));

    // Add the panel to the Content Pane
    add(panel);

    // Display the Window
    setVisible(true);
}

private class PlaylistListener implements ActionListener {

    int x = 0;

    public void actionPerformed(ActionEvent e) {

        try {
            playMusic(x);

        } catch (InterruptedException ex) {
            Logger.getLogger(JukeBoxWithArrays.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void playMusic(int x) throws InterruptedException {

        if (x > music.size()) {
            AudioPlayer.player.stop((InputStream) music.get(x));

        } else {
            AudioPlayer.player.start((InputStream) music.get(x));

        }
        Thread.sleep(5 * 60 * 1000); // I believe this is where I am running into my problem
        playMusic(x++);

    }

}

@SuppressWarnings("restriction")
public static void main(String[] args) throws Exception {

    JukeBoxWithArrays jbwa = new JukeBoxWithArrays();
    jbwa.pack();

}

我试图转储java堆,但它看起来对我来说都是胡言乱语。

这是预期的行为吗?什么实际填补了记忆?到底是怎么回事?

1 个答案:

答案 0 :(得分:1)

如果那是您的MainActivity,那么您没有内存泄漏。

但是,如果你想确定内存监视器中有一个名为Initiate GB的按钮。单击它将运行垃圾收集器以释放未使用的引用。

在执行此操作时,您将看到内存降至初始值。