問題原因
當你在操作圖片或者其他大量文件數據時會出現:Failed to allocate a 38189038 byte allocation with 16777216 free bytes and 20MB until OOM 報錯.
為什么會出現這個報錯?原因很簡單.因為一個app的內存只有64MB,而你在操作需要更多內存的文件.這個時候app無法申請到內存就會報這個錯誤.
解決辦法
1.在清單文件中添加 android:largeHeap="true" 屬性,將APP的內存從64MB拓展成128MB
<application android:name=".app.App" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="demo" android:networkSecurityConfig="@xml/network_security_config" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme" android:largeHeap="true" tools:replace="android:label">
2.盡量減少使用圖片圖標,用矢量圖代替圖標圖片
3.盡快的釋放一些不需要的使用的內存占用
4.保證app沒有內存泄漏