问题
Android version : 8.0.0
When I call file.list, if I open the android studio profiler to monitor the memory, it is easy to OOM error, as follows:
java.lang.OutOfMemoryError: EnsureLocalCapacity
at java.io.UnixFileSystem.list0(Native Method)
at java.io.UnixFileSystem.list(UnixFileSystem.java:303)
at java.io.File.list(File.java:1122)
I try to find problems from the source code ,List ends up calling list0 ,
file.list -> UnixFileSystem.list() -> UnixFileSystem_list0 :
Java_java_io_UnixFileSystem_list0(JNIEnv *env, jobject this,
jobject file)
{
DIR *dir = NULL;
struct dirent64 *ptr;
struct dirent64 *result;
int len, maxlen;
jobjectArray rv, old;
jclass str_class;
str_class = JNU_ClassString(env);
CHECK_NULL_RETURN(str_class, NULL);
WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
dir = opendir(path);
} END_PLATFORM_STRING(env, path);
if (dir == NULL) return NULL;
ptr = malloc(sizeof(struct dirent64) + (PATH_MAX + 1));
if (ptr == NULL) {
JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
closedir(dir);
return NULL;
}
// ...
}
And, confusingly, the string that's returned here is "heap allocation failed" ,not “EnsureLocalCapacity” ,so I don't know where "EnsureLocalCapacity" from
here is my code :
private static final String LINUX_CURRENT_FD_NUM = "/proc/" + Process.myPid() + "/fd";
private final File mFdFile = new File(LINUX_CURRENT_FD_NUM);
private void getFdInfo(){
final String[] fdFileList = mFdFile.list();
}
mContext.registerActivityLifecycleCallbacks(new
Application.ActivityLifecycleCallbacks() {
// ...
@Override
public void onActivityStopped(Activity activity) {
getFdInfo();
}
// ...
});
At present, it is found that it occurs under the premise of enabling profiler monitoring. It is not clear whether other conditions will occur.
A few questions:
- How to solve this problem?
- Is this problem related to profiler? Does turning on profiler memory monitoring consume memory? How much does it cost?
- where can see the profiler memory monitoring related source code
来源:https://stackoverflow.com/questions/55643820/oom-error-occurred-when-the-android-studio-profile-was-opened