Memory 处理内存时出现问题,程序在内存中缓慢爬升,然后崩溃

Memory 处理内存时出现问题,程序在内存中缓慢爬升,然后崩溃,memory,processing,Memory,Processing,我正在制作一个正在处理的短游戏,尽管如果我让它运行太长时间,我会遇到程序崩溃的问题。当我打开程序时,内存慢慢上升到800MB,当它接近900MB时,它就会消失,然后慢慢下降。每当它达到500 MB左右时,程序就会崩溃。我对使用处理是相当陌生的,所以我可能只是忘记了一些东西,一个计数器正在失控,但我似乎无法找到问题所在。谢谢你的阅读 关于这个问题,我还没有弄清楚多少,我已经研究过“System.gc();”,但我不确定这是否是问题的一部分,以及如何实现它,正如我已经尝试过的那样 //Imports

我正在制作一个正在处理的短游戏,尽管如果我让它运行太长时间,我会遇到程序崩溃的问题。当我打开程序时,内存慢慢上升到800MB,当它接近900MB时,它就会消失,然后慢慢下降。每当它达到500 MB左右时,程序就会崩溃。我对使用处理是相当陌生的,所以我可能只是忘记了一些东西,一个计数器正在失控,但我似乎无法找到问题所在。谢谢你的阅读

关于这个问题,我还没有弄清楚多少,我已经研究过“System.gc();”,但我不确定这是否是问题的一部分,以及如何实现它,正如我已经尝试过的那样

//Imports
import processing.sound.*;

//Global variables
int gameState = 0; //Using an integer value just in case I want to add more maps in future updates
int backX = 1800; //Current background x value
float theta = 0.0; //Used to control sin wave for oscillation
int menuCounter = 0; //Main menu space bar counter
int menuSpaceSize = 25; //Space text size
int menuSpaceX = 90; //Space text menu
float menuSpaceModifier = 1.0; //Allows for space text movement
int mainMenuX = 50; //Main menu x position
color spaceColor = color(255); //Color the space text is (used for color changes on space press)
boolean[] keys = new boolean[4]; //WASD keys
PImage backImage; //Images
SoundFile select, bg, menubg; //Sound files
boolean bgPlay, menuPlay = false; //Music Playing boolean

//Setup function (Runs once)
void setup() {
  //Set frame name
  surface.setTitle("Game");

  //Size
  size(600, 600, P3D);

  //Load images
  backImage = loadImage("https://github.com/Attaxika/stuff/blob/master/BackgroundImage.png?raw=true");

  //Load sound files
  select = new SoundFile(this, "https://raw.githubusercontent.com/Attaxika/stuff/master/select.wav?raw=true");
  bg = new SoundFile(this, "https://raw.githubusercontent.com/Attaxika/stuff/master/bg.mp3?raw=true");
  menubg = new SoundFile(this, "https://raw.githubusercontent.com/Attaxika/stuff/master/menu.wav?raw=true");
}

//Draw function (Runs 60 times a second)
void draw() {
  //Reset opacity
  tint(255, 255);

  //Reset color
  fill(0);

  //Make smooth
  smooth();

  //Background
  background(0, 0, 0);

  //Framerate set
  frameRate(60);

  //If the game isn't running
  if(gameState == 0) {
    //Stop background music
    bg.stop();
    bgPlay = false;

    //Start menu music
    if(!menuPlay) {
      menuPlay = true;
      menubg.amp(1);
      menubg.loop();
    }

    //Main menu
    textSize(50);

    //y value for main menu text oscillation
    float y = (sin(theta) + 1 ) * 20;

    //Increment theta
    theta += 0.05;

    //Primary game menu
    fill(255, 0, 0);
    text("Game Menu", width/2 - 150, y + 75);

    //4th fill value represents alpha
    fill(255, 255, 255, 25);
    //Shadow Text
    text("Game Menu", width/2 - 155, y + 74);

    //Space to start text
    fill(spaceColor);
    textSize(menuSpaceSize);
    text("Space to Start", (width/2 - menuSpaceX) + menuSpaceModifier, 200);

    //Check for space pressed
    if(menuCounter > 0 && menuCounter < 30) {
      menuCounter += 1;
      menuSpaceSize = 30;
      menuSpaceX = 140;
      menuSpaceModifier += 20;
    }

    //Check for space timer
    if(menuCounter == 29) {
      gameState = 1;
    }
  }
  //If the game is running
  else if(gameState == 1) {
    //Music start
    if(!bgPlay) {
      //Stop menu music
      menuPlay = false;
      menubg.stop();

      bgPlay = true;
      bg.amp(.40);
      bg.loop();
    }

    //Background image
    imageMode(CENTER);
    image(backImage, backX, backImage.height / 2, backImage.width, backImage.height);
    backX -= 1;

    //Reset background x if it gets to the end
    if(backX <= 0) {
      backX = 1800;
    }
  }

  //Unknown gameState
  else {
    exit();
  }
}

//Key pressing (Keyboard input)
void keyPressed() {
  //Pressed space & is on menu
  if(key == ' ' && gameState == 0 && menuCounter == 0) {
    spaceColor = color(0, 255, 0);
    menuCounter += 1;
    select.play();
    bgPlay = true;
  }
}

在查看了我的代码和一些人的建议后,我发现这与多次渲染图像、从未清除的图像或声音文件无关,我发现我的问题来自使用P3D渲染器的我。我把我的尺码上的“P3D”去掉了();然后,我对该程序的内存使用稳定在200 MB以下,从未出现过峰值。(据我所知,如果您不想使用默认的2D渲染器,则使用一个形状,然后在该3D形状的顶部应用纹理是一个潜在的解决方案)

在查看了我的代码和一些人的建议后,我发现这与多次渲染图像、从未清除的图像或我的声音文件无关,我发现我的问题来自我使用的P3D渲染器。我把我的尺码上的“P3D”去掉了();然后,我对该程序的内存使用稳定在200 MB以下,从未出现过峰值。(据我所知,如果您不想使用默认的2D渲染器,那么使用一个形状然后在该3D形状上应用纹理是一个潜在的解决方案)

您可能需要添加语言标记。我看不出您的代码有任何明显的错误。你能把你的问题缩小到一个小范围吗?从空白草图开始,只添加再现问题所需的代码。@J.D.我删除了所有与音频相关的内容,尽管它确实以180 MB的速度运行了一段时间,但很快在主菜单上的峰值超过了800,然后崩溃了。我让它运行在实际的滚动背景部分(同样,没有任何音乐),它做了同样的事情。你可能想添加语言标签。我没有看到你的代码有任何明显的错误。你能把你的问题缩小到一个小范围吗?从空白草图开始,只添加再现问题所需的代码。@J.D.我删除了所有与音频相关的内容,尽管它确实以180 MB的速度运行了一段时间,但很快在主菜单上的峰值超过了800,然后崩溃了。我让它运行在实际的滚动背景部分(同样,没有任何音乐),它做了同样的事情。有趣。奇怪的是,原来的程序在我的系统上运行了3/5次。。工藤是为了发现你自己!有趣。奇怪的是,原来的程序在我的系统上运行了3/5次。。工藤是为了发现你自己!
  clear();

  if(backImage!=null) {
    image(backImage, backX, backImage.height / 2, backImage.width, backImage.height);
  }

  if(millis() > 20000 && backImage!=null) {
    g.removeCache(backImage);
    backImage = null;
  }
  System.gc();