Here's a simplified version of the code I'm using
Java:
private native void malloc(int bytes); private native void free();
WITH
static char* blob = NULL; void Java_com_example_MyClass_malloc(JNIEnv * env, jobject this, jint bytes) { blob = (char*) malloc(sizeof(char) * bytes); if (NULL == blob) { __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "Failed to allocate memory\n"); } else { char m[50]; sprintf(m, "Allocated %d bytes", sizeof(char) * bytes); __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, m); } } void Java_com_example_MyClass_free(JNIEnv * env, jobject this) { free(blob); blob = NULL; }
Now when I call malloc () from MyClass.java, I expect to see 32M of allocated memory and that I can observe this drop in available memory somewhere. I did not see any indication of this, however, in adb shell dumpsys meminfo or adb shell cat /proc/meminfo . I am new to C but have tons of Java experience. I want to allocate a bunch of memory outside the Dalvik heap (so it is not controlled by Android / dalvik) for testing purposes. Hackbod made me believe that Android currently does not set limits on the amount of memory allocated in the Native code, so this is apparently the right approach. Am I doing it right?
Ctrlf source share