简体   繁体   English

运行缓慢,内存使用率高

[英]Slow running, High memory usage

I'm writing a small text-based game for my class. 我正在为班级写一个小型的基于文本的游戏。 The navigation is simple, in which the player clicks buttons to enter a different part. 导航很简单,玩家可以单击按钮来输入其他部分。 All of these parts are separated into their own blocks of code. 所有这些部分都分成各自的代码块。 I've set up only two parts so far, each of them being able to reach one another through buttons. 到目前为止,我仅设置了两个部分,每个部分都可以通过按钮相互联系。 Basically: 基本上:

private void level1() {
//Stuff here, player clicks a button which runs "level2();"
}

private void level2() {
//Stuff here, player clicks a button which runs "level1();"
}

So that works just fine, but after some clicking back and forth between 1 & 2, the program starts to run very slow. 这样就可以了,但是在1和2之间来回点击一下之后,程序开始运行非常慢。 Task Manager reports about 700MB of memory usage. 任务管理器报告大约700MB的内存使用情况。

I'm sure there's something real obvious that I'm missing. 我敢肯定,我确实缺少一些明显的东西。 I'm looking for a way for the user to be able to click many buttons, many times, and not make the program use so much resources. 我正在寻找一种方法,使用户能够多次单击许多按钮,而又不会使程序占用太多资源。 Help would be very much appreciated. 帮助将不胜感激。

Edit: Some more code. 编辑:更多代码。 Choice1-3 are the names for the buttons. Choice1-3是按钮的名称。 Variables are already declared at the top of the program, and set up in the initialize part. 变量已经在程序顶部声明,并在初始化部分进行设置。 They're to be modified with each block of code, such as Scene1 and 2. 它们将与每个代码块一起修改,例如Scene1和2。

private void setScene1() // TITLE SCREEN
{
    TITLE.setText("Title Label");
    main.setText("Main text body for the game.");
    choice1.setText("Play");
    choice1.setBounds(10, 600, 996, 91); // This makes choice1 as large as
                                            // all three buttons combined.
    choice2.setEnabled(false);// There's only one option anyway.
    choice3.setEnabled(false);
    choice2.setVisible(false);
    choice3.setVisible(false);

    choice1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            setScene2();
        }
    });

}

private void setScene2() // CHARACTER GENERATION SCENE
{
    TITLE.setText("Character Generation");
    main.setEnabled(false); // Disable & Hide the main text window, as
                            // Chargen requires a nicer looking interface.
    main.setVisible(false);
    choice2.setEnabled(true);
    choice2.setVisible(true);
    choice1.setBounds(10, 600, 996, 34); //Resizing the bottom portion to fit two buttons
    choice2.setBounds(10, 645, 996, 34);
    choice1.setText("Continue");
    choice1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            // Nothing here for now
        }
    });
    choice2.setText("Back to the Title Screen");
    choice2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            main.setEnabled(true);
            main.setVisible(true);
            setScene1();
        }
    });

}

If the two methods continue to call each other you'll eventually eat through your memory: this is recursion. 如果这两个方法继续相互调用,您最终将在内存中吃饱:这是递归。 If you never return up the call tree it just gets arbitrarily deep until boom. 如果您从不返回调用树,则它会变得任意深度直到繁荣。

The problem seems to lie in the fact that you're adding an ActionListener every time you switch scenes. 问题似乎在于您在每次切换场景时都添加了一个ActionListener。 The previous ActionListener is never going away, but you're stacking more on top of it that do the same thing. 以前的ActionListener永远不会消失,但是您可以在上面堆叠更多的功能来完成相同的事情。 Each one performs the same action, so when you press to switch scene, all the ActionListners that it now has will also go to switch scene. 每个动作执行相同的动作,因此当您按一下切换场景时,它现在拥有的所有ActionListners也会切换场景。 Your CPU will go up because there's now 2^n ActionListeners waiting for input and there's that many sitting in memory. 您的CPU将会启动,因为现在有2 ^ n个ActionListeners等待输入,并且内存中有很多这样的输入。

You should add the ActionListeners in the Constructor or somewhere else. 您应该在构造函数或其他地方添加ActionListeners。 This way you add a new ActionListener each time (even if it's the same) you press the button. 这样,您每次按下按钮时都添加一个新的ActionListener(即使它是相同的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM