0

I'm trying to cause a leak using this native method from my app. I can see "Method returned." in my Logs but I don't seem to lose any RAM. I'm using(MemoryInfo.availMem / 1048576L) for tracking usage.

JNIEXPORT jstring JNICALL Java_com_app_native_Wrapper_causeLeak(JNIEnv *je, jclass jc, jint bytes) {
   char *p_array = calloc(bytes,sizeof(char));
   return (*je)->NewStringUTF(je, "Method returned.");
}

And trying to cause 10MB leak via this method:

Wrapper.causeLeak(10 * 1024 * 1024)

EDIT:

I'm doing this because I want to test my app in a low memory situation.

Endre Börcsök
  • 477
  • 1
  • 8
  • 19
  • Ok, that should be sufficient to cause a leak. Not sure why you're doing this or what your question is. – Gabe Sechan Feb 03 '17 at 16:02
  • I'm doing this because I want to test my app in a low memory situation. – Endre Börcsök Feb 03 '17 at 16:07
  • I'm not sure if MemoryInfo.availMem takes native usage into account or if its just Java heap usage. You may want to look at https://developer.android.com/reference/android/os/Debug.MemoryInfo.html as an alternate source of info – Gabe Sechan Feb 03 '17 at 16:11
  • Also check out http://stackoverflow.com/questions/2298208/how-do-i-discover-memory-usage-of-my-application-in-android – Gabe Sechan Feb 03 '17 at 16:13
  • I'm using [link](https://developer.android.com/reference/android/app/ActivityManager.MemoryInfo.html). It returns the available system memory. I used `adb shell dumpsys meminfo` to track my PID but I can't see any increment either. Thought the compiler might optimise the code so I added APP_OPTIM := debug to the Application.mk too. No luck so far. – Endre Börcsök Feb 03 '17 at 16:17
  • Like I said- I think (but am not sure) that the availMem on that api only accounts for memory available to the Java heap. This would be easily testable by calling it, allocating 10MB in Java, keeping a reference to it, then calling it again and looking for change. THe other API I pointed out breaks things down by native and Java heaps, so I know it takes into account the native. – Gabe Sechan Feb 03 '17 at 16:21
  • I have been using this variable to track available system RAM for a year now. It's always returned the amount of available physical RAM. My Nexus 5X returns 400-600MBs through that API which would be a lot of heap :). – Endre Börcsök Feb 03 '17 at 16:23

1 Answers1

0

I couldn't get this working but I found someone on GitHub who built a better approach. If someone ever need to test memory leaks use the repository here: https://github.com/T-Spoon/Android-Developer-Toolbelt

Endre Börcsök
  • 477
  • 1
  • 8
  • 19