Question

the activity im calling "recreate()" on contains a surfaceview in it's xml layout, and it looks like i'm having trouble releasing memory from the bitmapfactory used by the surfaceview.

each time i recreate it decodes 10 bitmaps each at about 5-10KB.

i tried setting all of my bitmaps to null and running system.gc before every recreation but that didn't help.

i'm just trying to recreate my surface view periodically but i can't find a better way than recreating the activity it's in.

is there a more effective way to reinitialize my surface view, and if not, can you tell me what's going on with my garbage collection from this logcat?

04-28 03:39:05.368: E/AndroidRuntime(1939): FATAL EXCEPTION: main
04-28 03:39:05.368: E/AndroidRuntime(1939): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.irksomesloth.botbreaker/com.irksomesloth.botbreaker.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class <unknown>
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3740)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.app.ActivityThread.access$700(ActivityThread.java:141)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.os.Looper.loop(Looper.java:137)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.app.ActivityThread.main(ActivityThread.java:5103)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at java.lang.reflect.Method.invokeNative(Native Method)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at java.lang.reflect.Method.invoke(Method.java:525)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at dalvik.system.NativeStart.main(Native Method)
04-28 03:39:05.368: E/AndroidRuntime(1939): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class <unknown>
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.view.LayoutInflater.createView(LayoutInflater.java:620)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.app.Activity.setContentView(Activity.java:1895)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at com.irksomesloth.botbreaker.MainActivity.onCreate(MainActivity.java:15)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.app.Activity.performCreate(Activity.java:5133)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
04-28 03:39:05.368: E/AndroidRuntime(1939):     ... 12 more
04-28 03:39:05.368: E/AndroidRuntime(1939): Caused by: java.lang.reflect.InvocationTargetException
04-28 03:39:05.368: E/AndroidRuntime(1939):     at java.lang.reflect.Constructor.constructNative(Native Method)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.view.LayoutInflater.createView(LayoutInflater.java:594)
04-28 03:39:05.368: E/AndroidRuntime(1939):     ... 23 more
04-28 03:39:05.368: E/AndroidRuntime(1939): Caused by: java.lang.OutOfMemoryError
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:503)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:356)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:379)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:409)
04-28 03:39:05.368: E/AndroidRuntime(1939):     at com.irksomesloth.botbreaker.GameView.<init>(GameView.java:30)
Était-ce utile?

La solution

Look at the bitmap recycle() function. You might also need to set the bitmap option to purgeable. Here is a snippet where I resize a bitmap and output to a JPEG:

BitmapFactory.Options options = new BitmapFactory.Options();;
options.inPurgeable = true;

Bitmap original = BitmapFactory.decodeByteArray(input , 0, input.length, options);
Bitmap resized = Bitmap.createScaledBitmap(original, NEW_WIDTH, NEW_HEIGHT, true);

ByteArrayOutputStream blob = new ByteArrayOutputStream();
resized.compress(Bitmap.CompressFormat.JPEG, 30, blob);

// recycle the memory
original.recycle();
resized.recycle();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top