react-nativeエラーjavaを解決します.lang.OutOfMemoryError: Could not allocate JNI Env

4162 ワード

問題の再現:


パッケージ生成用rnで書かれたアプリを携帯電話にインストールした後、初めてアプリに入ってから問題なく、再び入ったらフラッシュバックして、それからもう入れなくなったので、この問題は私を3、4日悩ませたのではないでしょうか.私のこの説明は読めますか.私は知的障害者ですか?

エラーコード:


コマンドを検索し、adb logcat AndroidRuntime:E*:Sでエラーを印刷したところ、メモリの漏洩の問題が見つかりました.
PS G:\RN1workspace\TigerGameApp> adb logcat AndroidRuntime:E *:S
--------- beginning of system
--------- beginning of main
--------- beginning of crash
08-16 19:27:42.223 23377 29132 E AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
08-16 19:27:42.223 23377 29132 E AndroidRuntime: Process: com.tigergameapp, PID: 23377
08-16 19:27:42.223 23377 29132 E AndroidRuntime: java.lang.OutOfMemoryError: Could not allocate JNI Env
08-16 19:27:42.223 23377 29132 E AndroidRuntime:        at java.lang.Thread.nativeCreate(Native Method)
08-16 19:27:42.223 23377 29132 E AndroidRuntime:        at java.lang.Thread.start(Thread.java:1063)
08-16 19:27:42.223 23377 29132 E AndroidRuntime:        at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:921)
08-16 19:27:42.223 23377 29132 E AndroidRuntime:        at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1339)
08-16 19:27:42.223 23377 29132 E AndroidRuntime:        at okhttp3.ConnectionPool.put(ConnectionPool.java:135)
08-16 19:27:42.223 23377 29132 E AndroidRuntime:        at okhttp3.OkHttpClient$1.put(OkHttpClient.java:149)
08-16 19:27:42.223 23377 29132 E AndroidRuntime:        at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:188)
08-16 19:27:42.223 23377 29132 E AndroidRuntime:        at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:129)
08-16 19:27:42.223 23377 29132 E AndroidRuntime:        at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:98)
08-16 19:27:42.223 23377 29132 E AndroidRuntime:        at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
08-16 19:27:42.223 23377 29132 E AndroidRuntime:        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
08-16 19:27:42.223 23377 29132 E AndroidRuntime:        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
08-16 19:27:42.223 23377 29132 E AndroidRuntime:        at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:109)
08-16 19:27:42.223 23377 29132 E AndroidRuntime:        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
08-16 19:27:42.223 23377 29132 E AndroidRuntime:        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
08-16 19:27:42.223 23377 29132 E AndroidRuntime:        at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
08-16 19:27:42.223 23377 29132 E AndroidRuntime:        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
08-16 19:27:42.223 23377 29132 E AndroidRuntime:        at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:124)
08-16 19:27:42.223 23377 29132 E AndroidRuntime:        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)

解決プロセス:


友人(フロントエンドの大物)に教えてもらった後、タイマー関数に問題があると位置づけ、いったいどのブロックの問題なのかを自分で考えて、コードに戻って調べ続けたところ、タイマーを設定した後、タイマーをクリアしていないことに気づき、どんな状況なのか分からず、タイマーをクリアするコードを注釈した.そこで注釈を解いて再パッケージしてインストールすると、問題は解決します.

RN中国語ネットの解釈:


多くのReact Nativeアプリケーションで致命的なエラー(フラッシュバック)が発生するのはタイマに関係することを見出した.具体的には、コンポーネントがアンインストールされた後もタイマは動作します.この問題を解決するには、 unmount (clearTimeout/clearInterval) を銘記するだけです.
import React, { Component } from "react";

export default class Hello extends Component {
  componentDidMount() {
    this.timer = setTimeout(() => {
      console.log("           this ");
    }, 500);
  }
  componentWillUnmount() {
    //    Un"m"ount m   

    //     this.timer,   clearTimeout  。
    //        timer,       ,           ,    clear
    this.timer && clearTimeout(this.timer);
  }
}