1

我有一个带有很多项目和图像的微调器的应用程序。

每次用户选择列表中的一项时,都会执行一定数量(1 到 5)的渲染脚本脚本来更改图像的像素。

在做出大约 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:

这是亮度对比过滤器。我想每次创建新的输入和输出分配和脚本对象都会占用内存。是否可以将一个分配用于一系列渲染脚本操作?例如,我从位图进行分配,然后对其应用亮度对比度过滤器,然后使用一些具有类似实现的不同过滤器?

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 对象的注释行都没有被注释,并且内存泄漏得更快。

该设备是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
}
4

1 回答 1

1

是的,每次创建一个新的 ScriptC 对象确实需要大量内存。我将它们移到类字段变量中,一切正常。

于 2013-03-21T06:53:19.693 回答