Android Expand Jni Local Reference Table
Solution 1:
You need to delete local references to classes as well as to objects.
Solution 2:
It looks like you're getting lots of instances of java.lang.Class
. The most common way to get these is by calling FindClass
. The name in the <> angle brackets is the name of the class that was looked up, so you should be looking for places where you do a lookup on RoutePoint
or BikeRoute
.
FindClass
can be fairly expensive, so for frequently-used classes you want to call that during initialization and cache the result (as a global reference) for later use.
If you're running in a loop, it's a good idea to explicitly delete the local reference for any object returned. Expanding the local reference table beyond 512 entries isn't possible in Dalvik.
See also the JNI Tips document.
Solution 3:
I found the following technique useful if not horribly verbose, I created a class called Guardian as follows:
/*
* Guardian.h
*
* Created on: Jul 14, 2014
* Author: yaturner
*/#ifndef GUARDIAN_H_#define GUARDIAN_H_#define GLOGD(...) __android_log_print(ANDROID_LOG_DEBUG, tagName, __VA_ARGS__ )
{
classGuardian
{
private:
char* funcName;
char* tagName;
public:
Guardian(constchar*, constchar*);
virtual ~Guardian();
};
#endif/* GUARDIAN_H_ */
and
/*
* Guardian.cpp
*
* Created on: Jul 14, 2014
* Author: yaturner
*/#include"Guardian.h"Guardian(constchar* func, constchar* tag)
{
int len = strlen(func);
funcName = newchar[len+1];
strcpy(funcName, func);
len = strlen(func);
tagName = newchar[len+1];
strcpy(tagName, tag);
GLOGD("Entering %s", funcName);
}
~Guardian()
{
GLOGD("Exiting %s", funcName);
dumpLocalRefTable();
free(funcName);
free(tagName);
}
and
void dumpLocalRefTable()
{
JNIEnv* env = preamble();
jclass vm_class = env->FindClass("dalvik/system/VMDebug");
jmethodID dump_mid = env->GetStaticMethodID( vm_class, "dumpReferenceTables", "()V" );
env->CallStaticVoidMethod( vm_class, dump_mid );
env->DeleteLocalRef(vm_class);
}
Then at the beginning of each of my JNI methods I instanciated the Guardian class, what I got was a massive logcat with each method entry and exit plus the ref table. By examining the log I could look for changes in the table and determine what method caused them. Painful but it worked.
Post a Comment for "Android Expand Jni Local Reference Table"