patternjavaMinor
Optimizing garbage collection for local objects
Viewed 0 times
localobjectscollectionoptimizingforgarbage
Problem
I am trying to make a 3D application with OpenGL/LWJGL at the moment, but I am now already hitting some limits on the JVM, most likely due to a configuration that needs to be done.
I have the following code in the render loop:
The most important part is
```
public class Matrix4f {
private final static float EPSILON = 0.01f;
private final float[] elements = new float[16];
public Matrix4f() {
}
public Matrix4f(final float[] elements) {
System.arraycopy(elements, 0, this.elements, 0, 16);
}
public Matrix4f multiply(final Matrix4f other) {
float[] a = getElements();
float[] b = other.getElements();
return new Matrix4f(new float[] {
a[0] b[0] + a[4] b[1] + a[8] b[2] + a[12] b[3],
a[1] b[0] + a[5] b[1] + a[9] b[2] + a[13] b[3],
I have the following code in the render loop:
@Override
protected void render(final double msDelta) {
glClearColor(0.0f, 0.25f, 0.0f, 1.0f);
glClearDepthf(1f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
testProgram.use();
for (int i = 0; i < 2400; i++) {
float fVar = i + currentTime / 1000f * 0.3f;
modelviewMatrix = Matrix4f.multiply(
Matrix4f.translate(0.0f, 0.0f, -8.0f), //translate
Matrix4f.rotate(currentTime / 1000f * 45.0f, 0.0f, 1.0f, 0.0f), //rotate around Y
Matrix4f.rotate(currentTime / 1000f * 21.0f, 1.0f, 0.0f, 0.0f), //rotate around X
Matrix4f.translate(
(float)Math.sin(2.1f * fVar) * 2.0f,
(float)Math.cos(1.7f * fVar) * 2.0f,
(float)Math.sin(1.3f * fVar) * (float)Math.cos(1.5f * fVar) * 2.0f
) //translate
);
glUniformMatrix4(MODELVIEW_LOCATION, false, modelviewMatrix.asFloatBuffer());
glDrawArrays(GL_TRIANGLES, 0, 36);
}
}The most important part is
Matrix4f (My own implementation, not LWJGL's one... I don't know how I could make it more clear without renaming it to something terrible):```
public class Matrix4f {
private final static float EPSILON = 0.01f;
private final float[] elements = new float[16];
public Matrix4f() {
}
public Matrix4f(final float[] elements) {
System.arraycopy(elements, 0, this.elements, 0, 16);
}
public Matrix4f multiply(final Matrix4f other) {
float[] a = getElements();
float[] b = other.getElements();
return new Matrix4f(new float[] {
a[0] b[0] + a[4] b[1] + a[8] b[2] + a[12] b[3],
a[1] b[0] + a[5] b[1] + a[9] b[2] + a[13] b[3],
Solution
modelviewMatrix is not available for garbage collection until the render() method is complete. It is not clear from the code where this variable is actually defined and why it is not declared in the render() method.
My suggestion is to move the code inside the for loop into its own method, lets call it renderIteration. Any objects created in the renderIteration() method and not returned to the caller will be available for GC when the renderIteration() method completes.
Cheers.
My suggestion is to move the code inside the for loop into its own method, lets call it renderIteration. Any objects created in the renderIteration() method and not returned to the caller will be available for GC when the renderIteration() method completes.
Cheers.
Context
StackExchange Code Review Q#39508, answer score: 2
Revisions (0)
No revisions yet.