使用Kotlin :: class.java.canonicalName时出现OutOfMemory异常

时间:2018-01-03 18:56:15

标签: android kotlin

运行此行代码时出现Out Of Memory异常

private val TAG = KotlinFilePicker()::class.java.canonicalName

并且在禁用时它工作正常

Throwing OutOfMemoryError "Failed to allocate a 170 byte allocation with 68 free bytes and 68B until OOM" (recursive case)
01-04 00:14:55.044 20584-20584/com.androidbuffer.kotlinfilepickersample E/art: "main" prio=5 tid=1 Runnable
01-04 00:14:55.044 20584-20584/com.androidbuffer.kotlinfilepickersample E/art:   | group="main" sCount=0 dsCount=0 obj=0x7581c250 self=0xb4f07800
01-04 00:14:55.044 20584-20584/com.androidbuffer.kotlinfilepickersample E/art:   | sysTid=20584 nice=-11 cgrp=apps sched=0/0 handle=0xb6fd7ec8
01-04 00:14:55.044 20584-20584/com.androidbuffer.kotlinfilepickersample E/art:   | state=R schedstat=( 0 0 0 ) utm=1394 stm=35 core=1 HZ=100
01-04 00:14:55.044 20584-20584/com.androidbuffer.kotlinfilepickersample E/art:   | stack=0xbe57a000-0xbe57c000 stackSize=8MB
01-04 00:14:55.044 20584-20584/com.androidbuffer.kotlinfilepickersample E/art:   | held mutexes= "mutator lock"(shared held)
01-04 00:14:55.044 20584-20584/com.androidbuffer.kotlinfilepickersample E/art:     at android.support.v4.app.FragmentHostCallback.<init>(FragmentHostCallback.java:46)
01-04 00:14:55.044 20584-20584/com.androidbuffer.kotlinfilepickersample E/art:     at android.support.v4.app.FragmentHostCallback.<init>(FragmentHostCallback.java:63)
01-04 00:14:55.044 20584-20584/com.androidbuffer.kotlinfilepickersample E/art:     at android.support.v4.app.FragmentActivity$HostCallbacks.<init>(FragmentActivity.java:871)
01-04 00:14:55.044 20584-20584/com.androidbuffer.kotlinfilepickersample E/art:     at android.support.v4.app.FragmentActivity.<init>(FragmentActivity.java:100)
01-04 00:14:55.044 20584-20584/com.androidbuffer.kotlinfilepickersample E/art:     at android.support.v7.app.AppCompatActivity.<init>(AppCompatActivity.java:61)
01-04 00:14:55.044 20584-20584/com.androidbuffer.kotlinfilepickersample E/art:     at com.androidbuffer.kotlinfilepicker.KotlinFilePicker.<init>(KotlinFilePicker.kt:19)
01-04 00:14:55.044 20584-20584/com.android

有人可以用这种方式指出出错了吗

2 个答案:

答案 0 :(得分:3)

不应该

private val TAG = KotlinFilePicker::class.java.canonicalName

代替?

由于您的stacktrace声明如下:

Failed to allocate a 170 byte allocation with 68 free bytes and 68B until OOM" (recursive case)

这意味着我们在这里有无尽的递归。仔细看看代码,我们看到你创建了一个类的实例,它试图创建TAG属性,然后创建KotlinFilePicker的另一个实例......直到所有内存耗尽

答案 1 :(得分:2)

您正在创建一个新的KotlinFilePicker(),它拥有自己的TAG,可以创建新的KotlinFilePicker()等。

要修复它,请使用KotlinFilePicker::class.java.canonicalName