OutOfMemory Error when animating with PGraphic images

i can see the code you have posted but not the actual image generation method. if the images can be generated quickly enough you can just do something like

PGraphics generatedImage;

void setup() {
  size(512, 512, P2D);  
  frameRate(10);
}

void generateImage() {
  if(generatedImage == null)
    generatedImage = createGraphics(width, height);
  else
    generatedImage.clear();
  
  generatedImage.beginDraw();
  int shapeCount = (int)random(5);
  //draw some random stuff
  for(int i = 0; i < shapeCount; i++) {
    int shapeType = (int)random(2);
    int randomX = (int)random(width);
    int randomY = (int)random(height);
    int randomW = (int)random(width / 4);
    int randomH = (int)random(height / 4);
    
    generatedImage.fill((int)random(255), (int)random(255), (int)random(255), 255);
        
    switch(shapeType) {
      case 0:
        generatedImage.rect(randomX, randomY, randomW, randomH);
        break;
      case 1:
        generatedImage.ellipse(randomX, randomY, randomW, randomH);
        break;
    }
  }
  generatedImage.endDraw();
}

void draw() {
  background(0);
  generateImage();
  image(generatedImage, 0, 0);
}

or if the method is too slow you can generate them in another thread* while the first batch is being shown and once shown swap the current batch with the fresh batch

*i’m not 100 percent sure about PGraphics drawing while in a thread but i think it should be fine i think it’s more a sync issue if you are drawing to screen from a thread whereas this way you would be drawing offscreen.