处理中的内存问题,程序在内存中缓慢爬升然后崩溃

Issue with memory in Processing, program slowly climbs in memory and then crashes

我正在 Processing 中制作一个简短的游戏,但如果我离开 运行 太久,我会遇到程序崩溃的问题。当我打开程序时,内存慢慢上升到 800 MB,然后当它接近 900 MB 时,它逐渐下降并慢慢下降。每当它达到 500 MB 左右时,程序就会崩溃。我对使用 Processing 还很陌生,所以我可能只是忘记了一些东西,计数器失控了,但我似乎无法找到问题所在。感谢阅读。

关于这个问题我还没有弄清楚,我已经研究过“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;
  }
}

预期的结果是它在很长一段时间内 运行 而不会占用内存到崩溃的程度。

编辑: 有人回复但删除了他们的回复,但我遵循了一些指示,但无济于事。我将以下代码添加到我的程序中,菜单屏幕现在 运行s 无限期地使用低内存,但进入实际游戏 (gameState = 1) 会导致它崩溃,并指出它在 "image( backImage, backX, backImage.height / 2, backImage.width, backImage.height);"

这是我在“void draw() {”

之后直接添加的内容
  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();

在查看了我的代码并查看了一些人的建议后发现这与多次渲染且从未被清除的图像或我的声音文件无关,我发现我的问题来自我使用 P3D 渲染器。我从 size() 中删除了 "P3D";然后我的程序内存使用量稳定在 200 MB 以下并且从未激增。 (据我所知,如果您不想使用默认的 2D 渲染器,使用形状然后在该 3D 形状上应用纹理是一个潜在的解决方案)