我有一个带有旋转器的应用程序,其中包含大量项目和图像。
每次用户选择列表中的项目时,都会执行一个(1到5)个renderscript脚本来更改图像的像素。
在做出大约30个选择后,应用程序崩溃并出错。
这是堆栈跟踪。
03-20 13:56:59.366: ERROR/RenderScript(3405): Created fewer than expected number of RS threads.
03-20 13:57:02.990: ERROR/AndroidRuntime(3405): FATAL EXCEPTION: main
java.lang.RuntimeException: createWindowSurface failed EGL_BAD_ALLOC
at android.view.HardwareRenderer$GlRenderer.createSurface(HardwareRenderer.java:1064)
at android.view.HardwareRenderer$GlRenderer.createEglSurface(HardwareRenderer.java:961)
at android.view.HardwareRenderer$GlRenderer.initialize(HardwareRenderer.java:787)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1502)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
我想我应该以某种方式关闭renderscript使用的不必要的线程或什么?
更新:
有时错误不同:
03-20 16:09:42.293: ERROR/bcc(24984): !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
03-20 16:09:42.293: ERROR/bcc(24984): rslAssert [frameworks/compile/linkloader/include/ELFObject.h:86] SHNCommonDataPtr && "Must init common data size before use!"
03-20 16:09:42.293: ERROR/bcc(24984): !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
或
03-20 15:46:27.315: ERROR/AndroidRuntime(19330): FATAL EXCEPTION: Thread-3582
android.renderscript.RSRuntimeException: Loading of ScriptC script failed.
at android.renderscript.ScriptC.<init>(ScriptC.java:60)
at com.gfranq.android.ScriptC_BrightnessContrast.<init>(ScriptC_BrightnessContrast.java:41)
at com.gfranq.android.filters.logic.ImageFilter.brightnessContrastRs(ImageFilter.java:1438)
它指向
行 ScriptC_BrightnessContrast script = new ScriptC_BrightnessContrast(rs, context.getResources(),
R.raw.brightnesscontrast);
更新2:
这是Brightness conrast过滤器。 我想每次吃内存时都会创建新的进出分配和Script对象。是否可以对一系列renderscript操作使用一个分配?例如,我是从位图进行分配,然后是亮度转换滤波器,然后是一些具有类似实现的不同滤波器?
public Bitmap brightnessContrastRs(Bitmap bmIn, int brightness, int contrast, Context context)
{
Bitmap bmOut = Bitmap.createBitmap(bmIn.getWidth(), bmIn.getHeight(),
bmIn.getConfig());
// RenderScript rs = RenderScript.create(context);
Allocation allocIn;
allocIn = Allocation.createFromBitmap(rs, bmIn,
Allocation.MipmapControl.MIPMAP_NONE,
Allocation.USAGE_SCRIPT);
Allocation allocOut = Allocation.createTyped(rs, allocIn.getType());
ScriptC_BrightnessContrast script = new ScriptC_BrightnessContrast(rs, context.getResources(),
R.raw.brightnesscontrast);
script.set_in(allocIn);
allocIn.destroy();
script.set_out(allocOut);
script.set_script(script);
float rowContrast = ((100.0f + contrast) * (100.0f + contrast) / 10000.0f);
float rowBrightness = brightness / 255.f;
script.set_rowBrightness(rowBrightness);
script.set_rowContrast(rowContrast);
script.invoke_filter();
allocOut.copyTo(bmOut);
allocOut.destroy();
return bmOut;
}
除了在某些操作中,我还有相当大的int数组(360000 int),正如我所知,每次创建S criptC对象时,它都会为renderscript中定义的每个数组分配内存,因此内存可能会相当快地泄漏。 / p>
当我发布问题时,每次创建RenderScript对象的注释行都没有被注释,内存泄漏得更快。
该设备是nexus 4.
例如,这里是带有数组的RS文件(其他文件类似于此但可能不包含数组)
rs_allocation out;
rs_allocation in;
rs_script script;
float opacity;
float alphas[360000];
int width;
void root(const uchar4* v_in, uchar4* v_out, const void* usrData, uint32_t x,
uint32_t y)
{
float4 current = rsUnpackColor8888(*v_in);
current.a = alphas[width * y + x] * opacity;
*v_out = rsPackColorTo8888(current.r, current.g, current.b, current.a);
}
void filter()
{
#if !defined(RS_VERSION) || (RS_VERSION < 14)
rsForEach(script, in, out, 0);
#else
rsForEach(script, in, out);
#endif
}
答案 0 :(得分:1)
是的,每次创建一个新的ScriptC对象真的需要很多内存。我将它们移动到类字段变量,并且可以正常工作。