Better way of finding out where resources are going? - android

I have a memory leak in my app. So naturally I want to fix it. The issue is either I don't know how to use the DDMS and memory allocation tools or they are peices of crap (or both). So I'm wondering if there is another way I can figure out where all of my resources are being used or if someone can enlighten me on how to use the DDMS tools.
PS: Yes, I know I have to click the debug button in the DDMS and then cause a HPROF dump and/of use the Update heap button and do a GC. I can view the details of both but I can't see any of the objects I have created. In short I can't read what I'm viewing.
Any help or enlightenment is greatly appreciated.
~Aedon
Edit 1:
I added some logging throughout the drawing methods in my custom views. After some experimentation, I discoverd that the memory leak seams to be coming from this method.
/** Update the gauge independent static buffer cache for the background. */
private void regenerate() {
mNeedRegen = false;
// Prevent memory leaks by disposing of old bitmaps.
if (mBackground != null) { mBackground.recycle(); mBackground = null; }
// Our new drawing area
Log.d(TAG, getWidth() + "\t" + getHeight());
mBackground = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
Canvas backCanvas = new Canvas(mBackground);
float scale = (float)getWidth();
backCanvas.scale(scale, scale);
drawRim(backCanvas);
drawFace(backCanvas);
drawTitle(backCanvas);
if (!(this instanceof EmptySpace)) { drawGroupIcon(backCanvas); }
regenerateBackground(backCanvas);
}
Now after some fiddling around in my app, I managed to cause this error:
dalvikvm-heap E 195364-byte external allocation too large for this process.
2935 dalvikvm E Out of memory: Heap Size=4871KB, Allocated=2636KB, Bitmap Size=19528KB
2935 GraphicsJNI E VM won't let us allocate 195364 bytes
2935 AndroidRuntime D Shutting down VM
2935 dalvikvm W threadid=1: thread exiting with uncaught exception (group=0x400259f8)
2935 AndroidRuntime E FATAL EXCEPTION: main
2935 AndroidRuntime E java.lang.OutOfMemoryError: bitmap size exceeds VM budget
2935 AndroidRuntime E at android.graphics.Bitmap.nativeCreate(Native Method)
2935 AndroidRuntime E at android.graphics.Bitmap.createBitmap(Bitmap.java:574)
2935 AndroidRuntime E at com.android.appion.arm.widgets.GaugeBase.regenerate(GaugeBase.java:239)
2935 AndroidRuntime E at com.android.appion.arm.widgets.GaugeBase.onSizeChanged(GaugeBase.java:86)
2935 AndroidRuntime E at android.view.View.setFrame(View.java:7101)
2935 AndroidRuntime E at android.view.View.layout(View.java:7028)
2935 AndroidRuntime E at android.widget.GridView.setupChild(GridView.java:1316)
2935 AndroidRuntime E at android.widget.GridView.makeAndAddView(GridView.java:1222)
2935 AndroidRuntime E at android.widget.GridView.makeRow(GridView.java:265)
2935 AndroidRuntime E at android.widget.GridView.fillSpecific(GridView.java:463)
2935 AndroidRuntime E at android.widget.GridView.layoutChildren(GridView.java:1122)
2935 AndroidRuntime E at android.widget.AbsListView.onLayout(AbsListView.java:1147)
2935 AndroidRuntime E at android.view.View.layout(View.java:7034)
2935 AndroidRuntime E at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
2935 AndroidRuntime E at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
2935 AndroidRuntime E at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
2935 AndroidRuntime E at android.view.View.layout(View.java:7034)
2935 AndroidRuntime E at android.widget.SlidingDrawer.onLayout(SlidingDrawer.java:331)
2935 AndroidRuntime E at android.view.View.layout(View.java:7034)
2935 AndroidRuntime E at android.widget.RelativeLayout.onLayout(RelativeLayout.java:909)
2935 AndroidRuntime E at android.view.View.layout(View.java:7034)
2935 AndroidRuntime E at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
2935 AndroidRuntime E at android.view.View.layout(View.java:7034)
2935 AndroidRuntime E at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
2935 AndroidRuntime E at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
2935 AndroidRuntime E at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
2935 AndroidRuntime E at android.view.View.layout(View.java:7034)
2935 AndroidRuntime E at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
2935 AndroidRuntime E at android.view.View.layout(View.java:7034)
2935 AndroidRuntime E at android.view.ViewRoot.performTraversals(ViewRoot.java:1049)
2935 AndroidRuntime E at android.view.ViewRoot.handleMessage(ViewRoot.java:1744)
2935 AndroidRuntime E at android.os.Handler.dispatchMessage(Handler.java:99)
2935 AndroidRuntime E at android.os.Looper.loop(Looper.java:144)
2935 AndroidRuntime E at android.app.ActivityThread.main(ActivityThread.java:4937)
2935 AndroidRuntime E at java.lang.reflect.Method.invokeNative(Native Method)
2935 AndroidRuntime E at java.lang.reflect.Method.invoke(Method.java:521)
2935 AndroidRuntime E at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
2935 AndroidRuntime E at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
2935 AndroidRuntime E at dalvik.system.NativeStart.main(Native Method)
The error itself makes sense. I ran out of memory. What doesn't make sense is the bitmap I'm trying to make is 221px by 221px but aparently is 19528kb. If my math is right 221 * 221 = 48841 * 4 = 195364 bytes = 190.7kb. This doesn't make any sense at all. Anywho, please take a look and see what you can find. BTW, the line in question from the error is the following (from the regenerate method)
mBackground = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);

First, I think the Dalvik message is showing the wrong units whereas the GraphicsJNI error is correct:
dalvikvm E Out of memory: Heap
Size=4871KB, Allocated=2636KB, Bitmap
Size=19528KB 2935
GraphicsJNI E VM won't let us
allocate 195364 bytes
Keep in mind you can catch the memory error:
try{
mBackground = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
} catch(OutOfMemoryError e){
//do something
}
If your bitmaps are always the same size, I would recommend re-using them. If you are creating and destroying bitmaps fairly quickly, you run the risk of out-pacing the Garbage Collector and getting into this state.
--Edit--
As for your original question. I typically do the following when looking for a memory leak.
Initialization Steps
Make sure your app is set to be debuggable in the manifest
Launch your app, open DDMS and select your application.
Select the Allocation Tracker view and Start Tracking
Analysis
Let your app run, or perform some specific activities in your app that are causing memory issues.
Click "Get Allocations" to update the tracker. You can now stop tracking if you like.
In the filter, type your application name to narrow down the results.
You need to think about what you are seeing here and whether it makes sense for your application. Should there be 1000's of small memory allocations? Most likely not. At this point you can click on an allocation and see exactly which method and line number is causing the allocation.
Another route to take is to enable heap updates for your app and use the heap explorer to track allocations. The heap explorer does not tell you where the allocation is coming from, but what it will tell you is that for instance:
The number of 1k allocations is
growing and never shrinking.
You can then go into the Allocation Tracker and sort allocations by size to see where you are performing 1k allocations.

I like to use the allocation tracker tool.
http://android-developers.blogspot.com/2009/02/track-memory-allocations.html
I realize this isn't what you were asking about...
I've found the following documentation on Android memory leaks to be worth reading.
http://developer.android.com/resources/articles/avoiding-memory-leaks.html
...as well as the other posts on optimization at http://android-developers.blogspot.com/search/label/Optimization

Related

Android release APK fails immediately on launch on mobile device

The Android release APK crashes on launch when I install it on mobile device. It works fine on Android Emulator. Also, debug builds work fine on Emulator.
I’ve tried the methods in the below link suggested by #bhardman, #gderaco + #sacchykhedkar and #alvelig so far with no success.
https://github.com/facebook/react-native/issues/18357
Environment:
package.json
…
"dependencies": {
"react": "^16.6.3",
"react-dom": "^16.8.1",
"react-native": "0.58.4",
"react-native-gesture-handler": "^1.0.15",
"react-native-image-pan-zoom": "^2.1.11",
"react-navigation": "^3.2.1"
},
…
android/ build.gradle
…
buildscript {
ext {
buildToolsVersion = "28.0.2"
minSdkVersion = 16
compileSdkVersion = 28
targetSdkVersion = 27
supportLibVersion = "28.0.0"
}
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
}
}
…
App.js
…
import React, {Component} from 'react';
import { Platform, StyleSheet, Text, View, Image, Dimensions, Button, TouchableHighlight, WebView, ViewPropTypes } from 'react-native';
import { createStackNavigator, createAppContainer, StackActions, NavigationActions, withNavigation } from "react-navigation";
type Props = {};
export class App1 extends Component {
render() {
return (
<View style={styles.container}>
<TopMenu1 navigation={this.props.navigation} />
<View1Map />
</View>
);
}
}
…
Expected behaviour:
You open the app on your mobile device. And the app opens.
Actual behaviour:
App fails to launch on Android phone. Error message:
Unfortunately, [APP Name] had stopped.
Error messages (from logs.txt file generated from the Android phone):
I can't tell which lines are relevant. So I searched with the app name "sampleapp".
02-14 08:12:51.252 1062 1940 I ActivityManager: Killing 26119:com.android.defcontainer/u0a17 (adj 15): empty #17
02-14 08:12:51.260 1062 2363 I ActivityManager: Recipient 26119
02-14 08:12:51.270 1062 1385 I ActivityManager: Recipient 26671
02-14 08:12:51.295 1062 1698 D StatusBarManagerService: swetImeWindowStatus vis=0 backDisposition=0
02-14 08:12:51.341 1062 1940 D PMS : releaseWL(23288de): PARTIAL_WAKE_LOCK *launch* 0x1 WorkSource{10256}
02-14 08:12:51.410 1062 1385 I ActivityManager: Process com.htc.videocenter (pid 26671) has died
02-14 08:12:52.438 26718 26718 E AndroidRuntime: FATAL EXCEPTION: main
02-14 08:12:52.438 26718 26718 E AndroidRuntime: Process: com.sampleapp, PID: 26718
02-14 08:12:52.438 26718 26718 E AndroidRuntime: java.lang.IllegalStateException: Unable to create layer for ReactViewGroup
02-14 08:12:52.438 26718 26718 E AndroidRuntime: at android.os.MessageQueue.nativePollOnce(Native Method)
02-14 08:12:52.438 26718 26718 E AndroidRuntime: at android.os.MessageQueue.next(MessageQueue.java:323)
02-14 08:12:52.438 26718 26718 E AndroidRuntime: at android.os.Looper.loop(Looper.java:144)
02-14 08:12:52.438 26718 26718 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5845)
02-14 08:12:52.438 26718 26718 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
02-14 08:12:52.438 26718 26718 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
02-14 08:12:52.438 26718 26718 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
02-14 08:12:52.446 1062 2435 W ActivityManager: Force finishing activity com.sampleapp/.MainActivity
02-14 08:12:52.498 1062 2435 D PMS : acquireWL(f07ef11): PARTIAL_WAKE_LOCK *launch* 0x1 1062 1000 WorkSource{10256}
02-14 08:12:52.563 1062 1081 D StatusBarManagerService: setSystemUiVisibility(0x8000)
02-14 08:12:52.563 1062 1081 D StatusBarManagerService: disable:userId=0 what=0x0 which=0x1 pkg=Window{f67b705 u0 Application Error: com.sampleapp}
02-14 08:12:52.563 1062 1081 D StatusBarManagerService: hiding MENU key
02-14 08:12:52.832 1062 1184 D PMS : releaseHCC(967c58c): CPU_MIN_NUM ActivityManager-MultiCore-Num 0x8000 null
02-14 08:12:52.832 1062 1184 D PMS : releaseHCC(5a7f4d5): CPU_MIN_FREQ ActivityManager-MultiCore-Freq 0x2000 null
02-14 08:12:52.999 1062 1080 W ActivityManager: Activity pause timeout for ActivityRecord{858d360 u0 com.sampleapp/.MainActivity t2027 f}
02-14 08:12:53.108 1062 1072 D PMS : releaseWL(f07ef11): PARTIAL_WAKE_LOCK *launch* 0x1 WorkSource{2897}

Release application always crashing on splash screen

I'm facing an issue with React Native release builds. Either on iPhone or Android, my application works fine in debug mode, but after building with release configuration, the app always crashes on the splash screen.
On Android, I'm getting an OutOfMemory in the log triggered by WebSocketReader.java. Here is the Android log :
07-26 10:57:18.639 5893 7623 E AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
07-26 10:57:18.639 5893 7623 E AndroidRuntime: Process: com.schooltoring, PID: 5893
07-26 10:57:18.639 5893 7623 E AndroidRuntime: java.lang.OutOfMemoryError: Failed to allocate a 10628794 byte allocation with 4409280 free bytes and 4MB until OOM
07-26 10:57:18.639 5893 7623 E AndroidRuntime: at java.lang.StringFactory.newStringFromBytes(StringFactory.java:79)
07-26 10:57:18.639 5893 7623 E AndroidRuntime: at java.lang.StringFactory.newStringFromBytes(StringFactory.java:207)
07-26 10:57:18.639 5893 7623 E AndroidRuntime: at okio.Buffer.readString(Buffer.java:620)
07-26 10:57:18.639 5893 7623 E AndroidRuntime: at okio.Buffer.readUtf8(Buffer.java:591)
07-26 10:57:18.639 5893 7623 E AndroidRuntime: at okhttp3.internal.ws.WebSocketReader.readMessageFrame(WebSocketReader.java:222)
07-26 10:57:18.639 5893 7623 E AndroidRuntime: at okhttp3.internal.ws.WebSocketReader.processNextFrame(WebSocketReader.java:101)
07-26 10:57:18.639 5893 7623 E AndroidRuntime: at okhttp3.internal.ws.RealWebSocket.loopReader(RealWebSocket.java:262)
07-26 10:57:18.639 5893 7623 E AndroidRuntime: at okhttp3.internal.ws.RealWebSocket$2.onResponse(RealWebSocket.java:201)
07-26 10:57:18.639 5893 7623 E AndroidRuntime: at okhttp3.RealCall$AsyncCall.execute(RealCall.java:135)
07-26 10:57:18.639 5893 7623 E AndroidRuntime: at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
07-26 10:57:18.639 5893 7623 E AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
07-26 10:57:18.639 5893 7623 E AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
07-26 10:57:18.639 5893 7623 E AndroidRuntime: at java.lang.Thread.run(Thread.java:762)
Complete log : https://gist.github.com/SLedunois/7d801cb0e55f2ae98bdbc054b0a84e61 (Sorry about the link. Crash report are too long to be included in the body)
On iPhone, I don't really understand the crash report. I'm new at iOS development. Here is the iOS crash report :
https://gist.github.com/SLedunois/76eaa85dfebb2ceff1f97cb4723989f1
I think, the error is caused by :
Thread 12 Crashed:
0 libsystem_kernel.dylib 0x00000001815192ec __pthread_kill + 8
1 libsystem_pthread.dylib 0x00000001816ba288 pthread_kill$VARIANT$mp + 376
2 libsystem_c.dylib 0x0000000181487d0c abort + 140
3 Schooltoring 0x0000000100ccc734 0x100c78000 + 345908
4 CoreFoundation 0x0000000181a931c8 __handleUncaughtException + 828
5 libobjc.A.dylib 0x0000000180c4c8c8 _objc_terminate+ 35016 () + 112
6 Schooltoring 0x0000000100cb090c 0x100c78000 + 231692
7 libc++abi.dylib 0x0000000180c3d37c std::__terminate(void (*)+ 111484 ()) + 16
8 libc++abi.dylib 0x0000000180c3cccc __cxxabiv1::exception_cleanup_func+ 109772 (_Unwind_Reason_Code, _Unwind_Exception*) + 0
9 libobjc.A.dylib 0x0000000180c4c720 _objc_exception_destructor+ 34592 (void*) + 0
10 Schooltoring 0x0000000100d61af0 0x100c78000 + 957168
11 Schooltoring 0x0000000100d61638 0x100c78000 + 955960
12 libdispatch.dylib 0x0000000181384aa0 _dispatch_call_block_and_release + 24
13 libdispatch.dylib 0x0000000181384a60 _dispatch_client_callout + 16
14 libdispatch.dylib 0x000000018138e9b4 _dispatch_queue_serial_drain$VARIANT$mp + 608
15 libdispatch.dylib 0x000000018138f2fc _dispatch_queue_invoke$VARIANT$mp + 336
16 libdispatch.dylib 0x000000018138fcc8 _dispatch_root_queue_drain_deferred_wlh$VARIANT$mp + 340
17 libdispatch.dylib 0x0000000181398098 _dispatch_workloop_worker_thread$VARIANT$mp + 668
18 libsystem_pthread.dylib 0x00000001816b7e70 _pthread_wqthread + 860
19 libsystem_pthread.dylib 0x00000001816b7b08 start_wqthread + 4
All my builds are configured to be signed.
Does someone face a similar issue?
Try adding android:largeHeap="true" to your manifest under application:
Ex. <application android:label="YourApp" android:largeHeap="true"></application>
Found solution. The error was triggered because of the static image used in the application were too big.
See https://facebook.github.io/react-native/docs/images for more explanations.

Android throw DeadObjectException with LOG: Transaction failed on small parcel; remote process probably died

07-22 04:38:07.933 1579 3338 E JavaBinder: !!! FAILED BINDER TRANSACTION !!! (parcel size = 352)
07-22 04:38:07.933 1579 3338 W BroadcastQueue: Can't deliver broadcast to com.android.systemui (pid 2160). Crashing it.
07-22 04:38:07.934 1579 3338 W BroadcastQueue: Failure sending broadcast Intent { act=android.intent.action.TIME_TICK flg=0x50000014 (has extras) }
07-22 04:38:07.934 1579 3338 W BroadcastQueue: android.os.DeadObjectException: Transaction failed on small parcel; remote process probably died
07-22 04:38:07.934 1579 3338 W BroadcastQueue: at android.os.BinderProxy.transactNative(Native Method)
07-22 04:38:07.934 1579 3338 W BroadcastQueue: at android.os.BinderProxy.transact(Binder.java:618)
07-22 04:38:07.934 1579 3338 W BroadcastQueue: at android.app.ApplicationThreadProxy.scheduleRegisteredReceiver(ApplicationThreadNative.java:1211)
07-22 04:38:07.934 1579 3338 W BroadcastQueue: at com.android.server.am.BroadcastQueue.performReceiveLocked(BroadcastQueue.java:489)
07-22 04:38:07.934 1579 3338 W BroadcastQueue: at com.android.server.am.BroadcastQueue.deliverToRegisteredReceiverLocked(BroadcastQueue.java:702)
07-22 04:38:07.934 1579 3338 W BroadcastQueue: at com.android.server.am.BroadcastQueue.processNextBroadcast(BroadcastQueue.java:1002)
07-22 04:38:07.934 1579 3338 W BroadcastQueue: at com.android.server.am.BroadcastQueue.processNextBroadcast(BroadcastQueue.java:799)
07-22 04:38:07.934 1579 3338 W BroadcastQueue: at com.android.server.am.ActivityManagerService.finishReceiver(ActivityManagerService.java:19153)
07-22 04:38:07.934 1579 3338 W BroadcastQueue: at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:528)
07-22 04:38:07.934 1579 3338 W BroadcastQueue: at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2909)
07-22 04:38:07.934 1579 3338 W BroadcastQueue: at android.os.Binder.execTransact(Binder.java:565)
07-22 04:38:07.937 2160 2160 D AndroidRuntime: Shutting down VM
07-22 04:38:07.953 2160 2625 E JavaBinder: !!! FAILED BINDER TRANSACTION !!! (parcel size = 136)
--------- beginning of crash
07-22 04:38:07.972 2160 2160 E AndroidRuntime: FATAL EXCEPTION: main
07-22 04:38:07.972 2160 2160 E AndroidRuntime: Process: com.android.systemui, PID: 2160
07-22 04:38:07.972 2160 2160 E AndroidRuntime: android.app.RemoteServiceException: can't deliver broadcast
07-22 04:38:07.972 2160 2160 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1690)
07-22 04:38:07.972 2160 2160 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
07-22 04:38:07.972 2160 2160 E AndroidRuntime: at android.os.Looper.loop(Looper.java:160)
07-22 04:38:07.972 2160 2160 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6252)
07-22 04:38:07.972 2160 2160 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
07-22 04:38:07.972 2160 2160 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
07-22 04:38:07.972 2160 2160 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:788)
The error happened in the BroadcastQueue class, when it called scheduleRegisteredReceiver through Binder, the DeadObjectException throw. Like the LOG said: Transaction failed on small parcel; remote process probably died, but why RuntimeException throw in the com.android.systemui process if it already dead?
I finally found the root cause, it happened in the binder kernel.
For now, I discovered two reasons for what can cause a DeadObjectException to be thrown in BroadcastQueue and therafter a RemoteServiceException in ActivityThread in the app:
There are no more asynchronous space to execute the binder transaction when AMS sends a one-way binder call to ActivityThread in order to trigger BroadcastReceiver.onReceive.
Related code shown below:
kernel/msm-4.4/drivers/android/binder_alloc.c
290 if (is_async &&
291 alloc->free_async_space < size + sizeof(struct binder_buffer)) {
292 binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
293 "%d: binder_alloc_buf size %zd failed, no async space left\n",
294 alloc->pid, size);
295 eret = ERR_PTR(-ENOSPC);
296 goto error_unlock;
297 }
Therefore, this will not "end up destabilizing the system". It will only influences the application itself.
The user application had been force closed because BroadcastQueue send scheduleCrash binder call to ActivityThread. The root cause of this problem is that there are no binder buffer in the application side because some binder threads occupy most of it.
The bug can be triggered with the following steps:
Process1 sends large data (e.g. 980kB) to Process2, the Process2 need sleep for 30 seconds, and the large binder buffer will not be released.
Process1 sends a broadcast to Process2, consisting of e.g. 50kB data. That would go beyond the make the buffer capacity of 1016kB, since 980kB + 50kB is larger than the buffer capacity.
BroadcastQueue will throw a DeadObjectException and then pass scheduleCrash to ActivityThread in the application side.
Here is the code:
kernel/msm-4.4/drivers/android/binder_alloc.c
315 if (best_fit == NULL) {
...
341 pr_err("%d: binder_alloc_buf size %zd failed, no address space\n",
342 alloc->pid, size);
343 pr_err("allocated: %zd (num: %zd largest: %zd), free: %zd (num: %zd largest: %zd)\n",
344 total_alloc_size, allocated_buffers, largest_alloc_size,
345 total_free_size, free_buffers, largest_free_size);
346 eret = ERR_PTR(-ENOSPC);
347 goto error_unlock;
348 }
In conclusion, DeadObjectException can be thrown even if the application process haven't died.
The root cause is most likely because of full binder buffer for the application and does not influence the system.
So I think it is not necessary to make the application crash after catching a DeadObjectException in BroadcastQueue.
Basically everything Rick Ai's answer to their own question is correct but here is a a real world example:
If your app creates and registers a whole bunch of BroadcastReceiver instances all listening to the same action--perhaps due to a leak or bug in your app--then the ActivityManagerService in the system process will invoke android.app.IApplicationThread method scheduleRegisteredReceiver for each registered instance. Notice that the binder transaction for this particular method is oneway. Since it is oneway each invocation will return immediately and the calls to the binder driver will occur very rapidly before each transaction is complete thus effectively running them all in parallel.
Lets say you have 100 receivers in your app and the broadcast being received contains 20 KiB of data. Now you've got 2 MiB trying to pass through the binder driver and it will fail due to the limit of 1 MiB.
In kernel logs you will see:
binder: 1282:1298 transaction failed 29201/-28, size 28052-8 line 3072
So beware leaking BroadcastReceiver and beware oneway binder transactions. Note that apparently the AIDL file may not declare a method oneway but it may end up that way if the AIDL compiler decides it is possible.

how to easily convert code .java to .dex in android to directly execute via adb

I have just started learning android.
I want to run .java code directly via adb. I found a simple media player code here.
Using android studio. converted it into .class as mentioned here & linking android.jar by copying it into libs folder. Then converting into .dex using dx tool in SDK-Android\build-tools\25.0.3 by ./dx --dex --output=Beep.dex Beep.class.
Ran on my android running Android 7.1.2 in terminal ::
export CLASSPATH=./Beep.dex
app_process /system/bin Beep /system/media/audio/ringtones/Phobos.ogg
Got Output :: Killed .
Then viewed its output using logcat ::
07-30 12:55:09.760 11704 11704 D AndroidRuntime: >>>>>> START com.android.internal.os.RuntimeInit uid 0 <<<<<<
07-30 12:55:09.763 11704 11704 D AndroidRuntime: CheckJNI is OFF
07-30 12:55:09.834 11704 11704 D ICU : No timezone override file found: /data/misc/zoneinfo/current/icu/icu_tzdata.dat
07-30 12:55:09.886 11704 11704 I Radio-JNI: register_android_hardware_Radio DONE
07-30 12:55:09.896 11704 11704 D AndroidRuntime: Calling main entry Beep
07-30 12:55:09.901 11704 11704 I art : Thread[1,tid=11704,Native,Thread*=0x798ac40a00,peer=0x12c070d0,"main"] recursive attempt to load library "/system/lib64/libmedia_jni.so"
07-30 12:55:09.901 11704 11704 D MtpDeviceJNI: register_android_mtp_MtpDevice
07-30 12:55:09.901 11704 11704 I art : Thread[1,tid=11704,Native,Thread*=0x798ac40a00,peer=0x12c070d0,"main"] recursive attempt to load library "/system/lib64/libmedia_jni.so"
07-30 12:55:09.901 11704 11704 I art : Thread[1,tid=11704,Native,Thread*=0x798ac40a00,peer=0x12c070d0,"main"] recursive attempt to load library "/system/lib64/libmedia_jni.so"
07-30 12:55:09.976 534 11715 D NuPlayerDriver: notifyListener_l(0xeae20a80), (1, 0, 0), loop setting(0, 0)
07-30 12:55:09.982 11704 11718 E AndroidRuntime: *** FATAL EXCEPTION IN SYSTEM PROCESS: SetSubtitleAnchorThread
07-30 12:55:09.982 11704 11718 E AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
07-30 12:55:09.982 11704 11718 E AndroidRuntime: at android.media.SubtitleController.<init>(SubtitleController.java:105)
07-30 12:55:09.982 11704 11718 E AndroidRuntime: at android.media.MediaPlayer$2.run(MediaPlayer.java:2195)
07-30 12:55:09.982 11704 11718 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:751)
07-30 12:55:09.982 11704 11718 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
07-30 12:55:09.982 11704 11718 E AndroidRuntime: at android.os.Looper.loop(Looper.java:154)
07-30 12:55:09.982 11704 11718 E AndroidRuntime: at android.os.HandlerThread.run(HandlerThread.java:61)
07-30 12:55:09.992 1288 3721 W DropBoxManagerService: Dropping: system_server_crash (612 > 0 bytes)
07-30 12:55:09.994 11704 11718 I Process : Sending signal. PID: 11704 SIG: 9
07-30 12:55:10.011 534 2929 D NuPlayerDriver: reset(0xeae20a80) at state 4
07-30 12:55:10.011 534 2929 D NuPlayerDriver: notifyListener_l(0xeae20a80), (8, 0, 0), loop setting(1, 0)
07-30 12:55:10.012 534 11715 D NuPlayerDriver: notifyResetComplete(0xeae20a80)
07-30 12:55:10.897 3609 3670 W QCNEJ : |CORE| CNE received unexpected action: android.intent.action.BATTERY_CHANGED
What is the cause of this error ?
Is this the correct way of converting into .dex for running directly via adb.
Can't this be done using just dx tool or other simple & straightforward method ?

Memory error in Android after capturing images

In my Android application I'm trying to capture three images and upload this three images. I'm capturing two images one by one no problem but when I capture third images the application crashes with error:
09-26 16:17:31.398: ERROR/AndroidRuntime(24115): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
09-26 16:17:31.398: ERROR/AndroidRuntime(24115): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
09-26 16:17:31.398: ERROR/AndroidRuntime(24115): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:459)
09-26 16:17:31.398: ERROR/AndroidRuntime(24115): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:271)
09-26 16:17:31.398: ERROR/AndroidRuntime(24115): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:296)
How can I solve the error like this?

Categories

Resources