我使用RenderScript来旋转图像集。应用程序突然崩溃与致命信号11.日志中有100多个“D / RenderScript:从共享对象中丢失.rs.global_entries”消息。另外还要看到显着的内存增加。 这是什么原因?怎么解决呢?
我正在使用带有android 4.4.2的Samsung Galexy标签。我在下面列出了Gradle,Rs,Java和logcat输出。
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
minSdkVersion 18
targetSdkVersion 21
renderscriptTargetApi 18
renderscriptSupportModeEnabled true
#pragma version(1)
#pragma rs_fp_relaxed
#pragma rs java_package_name(com.models)
rs_allocation inImage;
int inWidth;
int inHeight;
uchar4 __attribute__ ((kernel)) rotate_270_clockwise (uchar4 in, uint32_t x, uint32_t y) {
uint32_t inX = inWidth - 1 - y;
uint32_t inY = x;
const uchar4 *out = rsGetElementAt(inImage, inX, inY);
return *out;
}
uchar4 __attribute__ ((kernel)) rotate_180_clockwise (uchar4 in, uint32_t x, uint32_t y) {
uint32_t inX = inWidth - 1 - x;
uint32_t inY = inHeight - 1 - y;
const uchar4 *out = rsGetElementAt(inImage, inX, inY);
return *out;
}
uchar4 __attribute__ ((kernel)) rotate_90_clockwise (uchar4 in, uint32_t x, uint32_t y) {
uint32_t inX = y;
uint32_t inY = inHeight - 1 - x;
const uchar4 *out = rsGetElementAt(inImage, inX, inY);
return *out;
}
uchar4 __attribute__ ((kernel)) rotate_0_clockwise (uchar4 in, uint32_t x, uint32_t y) {
uint32_t inX = x;
uint32_t inY = y;
const uchar4 *out = rsGetElementAt(inImage, inX, inY);
return *out;
}
Java代码
public Bitmap rotateBitmap(Bitmap bitmap, Activity activity, int angle, boolean recycle) {
Bitmap target = null;
try {
RenderScript rs = RenderScript.create(activity, RenderScript.ContextType.DEBUG);
ScriptC_rotator script = new ScriptC_rotator(rs);
script.set_inWidth(bitmap.getWidth());
script.set_inHeight(bitmap.getHeight());
Allocation sourceAllocation = Allocation.createFromBitmap(rs, bitmap,
Allocation.MipmapControl.MIPMAP_NONE,
Allocation.USAGE_SCRIPT);
if (recycle)
bitmap.recycle();
script.set_inImage(sourceAllocation);
//270 and 90
int targetHeight = bitmap.getWidth();
int targetWidth = bitmap.getHeight();
if (angle == 180 || angle == 0) {
targetHeight = bitmap.getHeight();
targetWidth = bitmap.getWidth();
}
Bitmap.Config config = bitmap.getConfig();
target = Bitmap.createBitmap(targetWidth, targetHeight, config);
final Allocation targetAllocation = Allocation.createFromBitmap(rs, target,
Allocation.MipmapControl.MIPMAP_NONE,
Allocation.USAGE_SCRIPT);
if (angle == 90)
script.forEach_rotate_90_clockwise(targetAllocation, targetAllocation);
else if (angle == 180)
script.forEach_rotate_180_clockwise(targetAllocation, targetAllocation);
else if (angle == 270)
script.forEach_rotate_270_clockwise(targetAllocation, targetAllocation);
else
script.forEach_rotate_0_clockwise(targetAllocation, targetAllocation);
targetAllocation.copyTo(target);
rs.destroy();
} catch (Exception e) {
Log.d("myApp", "rotateBitmap- render script " + e.getMessage());
}
return target;
}
日志
04-19 22:04:02.219 30377-30377/com.champ D/myApp﹕ processActionUp-scaleDetector.isInProgress()
04-19 22:04:04.259 30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
04-19 22:04:04.259 30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
04-19 22:04:04.269 30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
04-19 22:04:04.269 30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
04-19 22:04:04.279 30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
04-19 22:04:04.279 30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
04-19 22:04:04.289 30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
04-19 22:04:04.319 30377-30377/com.champ D/myApp﹕ processActionUp
04-19 22:04:04.319 30377-30377/com.champ D/myApp﹕ processActionUp-scaleDetector.isInProgress()
04-19 22:04:07.389 30377-30377/com.champ D/dalvikvm﹕ GC_FOR_ALLOC freed 2335K, 9% free 33300K/36444K, paused 26ms, total 26ms
04-19 22:04:07.389 30377-30377/com.champ I/dalvikvm-heap﹕ Grow heap (frag case) to 33.607MB for 320336-byte allocation
04-19 22:04:07.409 30377-30377/com.champ D/dalvikvm﹕ GC_FOR_ALLOC freed 3K, 9% free 33609K/36760K, paused 20ms, total 20ms
04-19 22:04:07.419 30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
04-19 22:04:07.479 30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
04-19 22:04:07.489 30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
04-19 22:04:07.489 30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
04-19 22:04:07.499 30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
04-19 22:04:07.509 30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
04-19 22:04:07.509 30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
04-19 22:04:07.519 30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
04-19 22:04:07.529 30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
04-19 22:04:07.529 30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
04-19 22:04:07.559 30377-30377/com.champ D/myApp﹕ processActionUp
04-19 22:04:07.559 30377-30377/com.champ D/myApp﹕ processActionUp-scaleDetector.isInProgress()
04-19 22:04:11.709 30377-30377/com.champ D/dalvikvm﹕ GC_FOR_ALLOC freed 1496K, 12% free 32489K/36760K, paused 20ms, total 20ms
04-19 22:04:11.759 30377-30377/com.champ D/dalvikvm﹕ GC_FOR_ALLOC freed 20K, 11% free 32843K/36760K, paused 18ms, total 19ms
04-19 22:04:11.759 30377-30377/com.champ I/dalvikvm-heap﹕ Grow heap (frag case) to 33.221MB for 383696-byte allocation
04-19 22:04:11.779 30377-30386/com.champ D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 11% free 33217K/37136K, paused 16ms, total 17ms
04-19 22:04:11.779 30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
04-19 22:04:11.789 30377-30763/com.champ A/libc﹕ Fatal signal 11 (SIGSEGV) at 0x66082000 (code=1), thread 30763 (AsyncTask #4)
答案 0 :(得分:3)
停止创建RenderScript上下文(android.RenderScript),并在每次调用旋转*()函数时加载脚本(即ScriptC_rotator)。您应该缓存这些对象,因为它们也不是轻量级的。这应该减少日志记录,并减少内存碎片(因为你继续加载/卸载脚本)。
SIGSEGV有点令人困惑,但可能是因为你的.rs文件中的内存访问超出范围。我看到你正在创建一个DEBUG上下文。当您捕获此日志时是否存在,或者是稍后添加的内容?考虑到由于旋转而交换宽度/高度,我认为你在.rs内核中使用了错误的Y / X值。考虑使用rsDebug打印出你正在编写的索引,你可能会看到它最终失败的地方(并且它确实超出了界限)。