We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I created some cool planetary gears with vectors and shapes (not PShapes) with success!
Demo here: https://youtu.be/RTZvgVu4LeU
I tried replacing the gear shapes with PShapes and when I do rotations or translations the memory used (used heap and heap size) keeps growing!
I used Visual VM to monitor and included a screen grab.
Why does memory grow when I rotate or translate PShapes? Is this a memory leak? Seems fine if I comment out the translations and rotations.
Sample code is something I was testing and a demo of growing heap:
// Author: GLV
// Date: 2018-03-04
// Version: 04
PShape s2;
int time_start;
PVector v1;
void settings()
{
size(600, 600, P2D);
}
void setup()
{
s2 = createShape();
s2.beginShape();
fill(102);
stroke(255);
strokeWeight(0);
s2.vertex(0, 0);
s2.vertex(0, 20);
s2.vertex(20, 20);
s2.vertex(20, 0);
s2.endShape(CLOSE);
time_start = millis();
v1 = new PVector(0, 0);
}
void draw()
{
background(0);
translate(width/2, height/2);
float radius = 20*2*40/TAU; // radius for equal spacing of teeth
noFill();
strokeWeight(3);
ellipse(0, 0, 2*radius, 2*radius);
v1.set(0, 20*2*40/TAU);
for (int i=0; i<=40; i++)
{
fill(255, 255, 0);
v1.rotate(TAU/40);
s2.translate(-10, -10); //Comment this line
s2.rotate(v1.heading()); //Comment this line
shape(s2, v1.x, v1.y);
s2.rotate(-v1.heading()); //Comment this line
s2.translate(10, 10); //Comment this line
}
if((millis()-time_start) >= 10000)
{
// System.gc();
// Runtime.getRuntime().gc();
time_start = millis();
println("Garbage Collection");
}
}
Answers
You could try commenting out lines to try and pinpoint the culprit
As I stated: "Seems fine if I comment out the translations and rotations."
I added "// Comment this line" to code.
I have had similar issues with PShapes with other code but only when I rotate or translate.
Are there any know issues with PShapes that I need to be aware of?
I have solved my PShape memory growing issues in the original example I provided:
Learning something new every day! :)
@GLV Just curious if pushMatrix/popMatrix could do the trick for you as well instead of resetting. I don't think resetting to be a good absolute solution as you might need to conserve some of those transformations... That is what I foresee in certain situations.
Kf
Your planetary gear vizualisation is really very nice.
Thanks everyone for comments and feedback! @koogs @kfrajer @Moxl
Some test code (below) I wrote to explore a few different ways of building my gears.
The most complicated was my first attempt building them with vectors! I learned a lot along the way...
Kf
@kfrajer I liked that!