summaryrefslogtreecommitdiff
path: root/graphics/java/android/renderscript/RenderScript.java
diff options
context:
space:
mode:
authorTim Murray <timmurray@google.com>2013-04-11 19:32:14 -0700
committerTim Murray <timmurray@google.com>2013-04-11 22:04:26 -0700
commit6dacf8355a0692b52c49f603f43317772cb36175 (patch)
treee655e80361cc4ce74d30e2ce1bb2c4614bd9002e /graphics/java/android/renderscript/RenderScript.java
parentb3a9872549137f6c3983609e48e5b2d4fc94a3d4 (diff)
Fix issues with GC thread.
bug 8600533 Change-Id: I356902ee9897daeb2c1090815beeb572234c81e7
Diffstat (limited to 'graphics/java/android/renderscript/RenderScript.java')
-rw-r--r--graphics/java/android/renderscript/RenderScript.java44
1 files changed, 29 insertions, 15 deletions
diff --git a/graphics/java/android/renderscript/RenderScript.java b/graphics/java/android/renderscript/RenderScript.java
index d5af2767bb40..bc6633daa15a 100644
--- a/graphics/java/android/renderscript/RenderScript.java
+++ b/graphics/java/android/renderscript/RenderScript.java
@@ -18,7 +18,9 @@ package android.renderscript;
import java.io.File;
import java.lang.reflect.Field;
+import java.util.concurrent.locks.*;
+import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
@@ -1042,8 +1044,11 @@ public class RenderScript {
RenderScript mRS;
boolean mRun = true;
- int currentSize = 0;
- final static int targetSize = 256*1024*1024; // call System.gc after 256MB of allocs
+ long currentSize = 0;
+ long targetSize; // call System.gc after 512MB of allocs
+
+ final Lock lock = new ReentrantLock();
+ final Condition cond = lock.newCondition();
GCThread(RenderScript rs) {
super("RSGCThread");
@@ -1052,29 +1057,37 @@ public class RenderScript {
}
public void run() {
+ ActivityManager am = (ActivityManager)mRS.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
+ ActivityManager.MemoryInfo meminfo = new ActivityManager.MemoryInfo();
+ am.getMemoryInfo(meminfo);
+ targetSize = (long)(meminfo.totalMem * .5f);
+
while(mRun) {
- boolean doGC = false;
- synchronized(this) {
- if (currentSize >= targetSize) {
- doGC = true;
- }
- }
- if (doGC == true) {
- System.gc();
- }
+ System.gc();
+ lock.lock();
try {
- sleep(1, 0);
- } catch(InterruptedException e) {
+ cond.awaitUninterruptibly();
+ } finally {
+ lock.unlock();
}
}
+
Log.d(LOG_TAG, "GCThread exiting.");
}
- public synchronized void addAllocSize(int bytes) {
+ public synchronized void addAllocSize(long bytes) {
currentSize += bytes;
+ if (currentSize >= targetSize) {
+ lock.lock();
+ try {
+ cond.signal();
+ } finally {
+ lock.unlock();
+ }
+ }
}
- public synchronized void removeAllocSize(int bytes) {
+ public synchronized void removeAllocSize(long bytes) {
currentSize -= bytes;
}
@@ -1186,6 +1199,7 @@ public class RenderScript {
nContextDeinitToClient(mContext);
mMessageThread.mRun = false;
mGCThread.mRun = false;
+ mGCThread.addAllocSize(0);
try {
mMessageThread.join();
mGCThread.join();