diff options
author | Sven Gothel <[email protected]> | 2010-10-14 18:39:42 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2010-10-14 18:39:42 +0200 |
commit | 6ced17f0325d5719e992b246ffd156e5b39694b4 (patch) | |
tree | c8618ebe347466e26c5ac8feb818b6844761121e /src/jogl/classes/com/jogamp/opengl/util | |
parent | 29999cf3b7616c8ab58f4483c672e30076fbb3e4 (diff) |
Fix: Memory consumption
Observing memory consumption showed:
1 - 'traceLock' debug stack traces (GLContextLock)
2 - massive Iterator usage
(1) is fixed, ie only enabled in DEBUG mode, like we have done in RecursiveLock before
(2) Using an Iterator on ArrayLists with a low element count < 100,
as it is usual in our use cases, is observed not to be faster
than accessing the elements via an index (-> TestIteratorIndexCORE.java ).
On the contrary, the index implementation was a bit faster.
Further more, these Iterators were massively used on the fly during animation,
hence their memory managment even impacts fluent processing/animation.
Recoded all animation related (display, surfaceUpdated, ..) loops using an index.
Diffstat (limited to 'src/jogl/classes/com/jogamp/opengl/util')
3 files changed, 67 insertions, 40 deletions
diff --git a/src/jogl/classes/com/jogamp/opengl/util/AWTAnimatorImpl.java b/src/jogl/classes/com/jogamp/opengl/util/AWTAnimatorImpl.java index e3aff61c6..9dd58bb57 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/AWTAnimatorImpl.java +++ b/src/jogl/classes/com/jogamp/opengl/util/AWTAnimatorImpl.java @@ -55,29 +55,36 @@ class AWTAnimatorImpl extends AnimatorImpl { public void display(AnimatorBase animator, boolean ignoreExceptions, boolean printExceptions) { - Iterator iter = animator.drawableIterator(); - while (animator.isAnimating() && !animator.getShouldStop() && !animator.getShouldPause() && iter.hasNext()) { - GLAutoDrawable drawable = (GLAutoDrawable) iter.next(); - if (drawable instanceof JComponent) { - // Lightweight components need a more efficient drawing - // scheme than simply forcing repainting of each one in - // turn since drawing one can force another one to be - // drawn in turn - lightweights.add(drawable); - } else { - try { - drawable.display(); - } catch (RuntimeException e) { - if (ignoreExceptions) { - if (printExceptions) { - e.printStackTrace(); + List drawables = animator.acquireDrawables(); + try { + for (int i=0; + animator.isAnimating() && !animator.getShouldStop() && !animator.getShouldPause() && i<drawables.size(); + i++) { + GLAutoDrawable drawable = (GLAutoDrawable) drawables.get(i); + if (drawable instanceof JComponent) { + // Lightweight components need a more efficient drawing + // scheme than simply forcing repainting of each one in + // turn since drawing one can force another one to be + // drawn in turn + lightweights.add(drawable); + } else { + try { + drawable.display(); + } catch (RuntimeException e) { + if (ignoreExceptions) { + if (printExceptions) { + e.printStackTrace(); + } + } else { + throw(e); } - } else { - throw(e); } } } + } finally { + animator.releaseDrawables(); } + if (lightweights.size() > 0) { try { SwingUtilities.invokeAndWait(drawWithRepaintManagerRunnable); diff --git a/src/jogl/classes/com/jogamp/opengl/util/AnimatorBase.java b/src/jogl/classes/com/jogamp/opengl/util/AnimatorBase.java index a54f6be57..24eee1875 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/AnimatorBase.java +++ b/src/jogl/classes/com/jogamp/opengl/util/AnimatorBase.java @@ -28,9 +28,11 @@ package com.jogamp.opengl.util; +import com.jogamp.common.util.locks.RecursiveLock; import com.jogamp.opengl.impl.Debug; import java.util.ArrayList; import java.util.Iterator; +import java.util.List; import javax.media.opengl.GLAnimatorControl; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLProfile; @@ -43,7 +45,8 @@ public abstract class AnimatorBase implements GLAnimatorControl { private static int animatorCount = 0; - protected volatile ArrayList/*<GLAutoDrawable>*/ drawables = new ArrayList(); + protected ArrayList/*<GLAutoDrawable>*/ drawables = new ArrayList(); + protected RecursiveLock drawablesLock = new RecursiveLock(); protected AnimatorImpl impl; protected String baseName; protected Thread thread; @@ -75,18 +78,24 @@ public abstract class AnimatorBase implements GLAnimatorControl { protected abstract String getBaseName(String prefix); public synchronized void add(GLAutoDrawable drawable) { - ArrayList newList = (ArrayList) drawables.clone(); - newList.add(drawable); - drawables = newList; - drawable.setAnimator(this); + drawablesLock.lock(); + try { + drawables.add(drawable); + drawable.setAnimator(this); + } finally { + drawablesLock.unlock(); + } notifyAll(); } public synchronized void remove(GLAutoDrawable drawable) { - ArrayList newList = (ArrayList) drawables.clone(); - newList.remove(drawable); - drawables = newList; - drawable.setAnimator(null); + drawablesLock.lock(); + try { + drawables.remove(drawable); + drawable.setAnimator(null); + } finally { + drawablesLock.unlock(); + } notifyAll(); } @@ -101,8 +110,13 @@ public abstract class AnimatorBase implements GLAnimatorControl { totalFrames++; } - public Iterator drawableIterator() { - return drawables.iterator(); + public List acquireDrawables() { + drawablesLock.lock(); + return drawables; + } + + public void releaseDrawables() { + drawablesLock.unlock(); } public long getCurrentTime() { diff --git a/src/jogl/classes/com/jogamp/opengl/util/AnimatorImpl.java b/src/jogl/classes/com/jogamp/opengl/util/AnimatorImpl.java index e4bf8d711..8f2715e0a 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/AnimatorImpl.java +++ b/src/jogl/classes/com/jogamp/opengl/util/AnimatorImpl.java @@ -44,20 +44,26 @@ class AnimatorImpl { public void display(AnimatorBase animator, boolean ignoreExceptions, boolean printExceptions) { - Iterator iter = animator.drawableIterator(); - while (animator.isAnimating() && !animator.getShouldStop() && !animator.getShouldPause() && iter.hasNext()) { - GLAutoDrawable drawable = (GLAutoDrawable) iter.next(); - try { - drawable.display(); - } catch (RuntimeException e) { - if (ignoreExceptions) { - if (printExceptions) { - e.printStackTrace(); + List drawables = animator.acquireDrawables(); + try { + for (int i=0; + animator.isAnimating() && !animator.getShouldStop() && !animator.getShouldPause() && i<drawables.size(); + i++) { + GLAutoDrawable drawable = (GLAutoDrawable) drawables.get(i); + try { + drawable.display(); + } catch (RuntimeException e) { + if (ignoreExceptions) { + if (printExceptions) { + e.printStackTrace(); + } + } else { + throw(e); } - } else { - throw(e); } } + } finally { + animator.releaseDrawables(); } } |