Memory Usage Optimization

I’m currently working on a project that needs the ability to draw 500 * 500 * 256 boxes. [https://github.com/Mr-Pyro/3DSV]. I am running out of ram. The PShape storing just the boxes alone seems to be approaching 2-3GB even after optimizing the code to only draw sides of a shape if not bordering a nearby box. I am also not drawing the bottom and tops of the boxes. Even with these optimizations I am still quickly running out of memory. Is there a better way, or more ways to optimize this?

This is the relevant class: ThreadedShapeCompositor.java

1 Like

Hi, did you try to use only one PShape object?

Instead of storing all your PShape object in an array you can defin only 1 PShape object that you will move and resize for all the PShape that you want to draw.

Something like this:

PShape rect;

void setup() {
  size(1000, 500);
  
  background(20);
  
  rect = createShape(RECT, 0, 0, 80, 80);
}


void draw() {
  background(20);
  
  fill(200);
  
  for (int i = 0; i <5; i++) {
    pushMatrix();
    translate(i*150, i*100);
    shape(rect);
    popMatrix();
    
  }
  
}

That was the way I was attempting to do it originally, but looping through 500 * 500 * 256 different positions each frame causes severe lag. Currently I am looping through all positions 1 time and adding them as children to a PShape object and then just displaying that PShape object each frame. That PShape object is becoming excessively large (I would assume it is because of sizeof(vertices) * 8 for each box * 500 * 500 * 256 == A very large number). Should I create boxes out of lines instead of quads and further optimize (avoid overlaps and other unnecessary geometry) or …?

What is the goal of your application and how will it be used?
Maybe you don’t need to display all of them everytime?

1 Like

Awesome, I’ll take a look at that. Also I just looked at the code for PShape and realized that it stores children as an array of PShape objects. This means that the PShape objects are not garbage collected and the variables in those objects (which is not needed) is kept. I am going to try to rewrite my code to just add the vertices to the main PShape object instead of child PShape objects.

1 Like