Caused by: java.lang.OutOfMemoryError: Failed to allocate a 38428990 byte allocation with 23734224

來源:互聯網
上載者:User

  關於記憶體溢出的問題,此間一個網路請求,返回json資料

HttpURLConnection connection = (HttpURLConnection) httpUrl        .openConnection();//設定參數  請求為get請求connection.setReadTimeout(5000);connection.setRequestMethod("GET");//connection.getInputStream()得到位元組輸入資料流,InputStreamReader從位元組到字元的橋樑,外加封裝字元流BufferedReader bufferedReader = new BufferedReader(        new InputStreamReader(connection.getInputStream()));//建立字串容器StringBuffer sb = new StringBuffer();String str = "";//行讀取while ((str = bufferedReader.readLine()) != null) {    // 當讀取完畢,就添加到容器中    sb.append(str);}//測試是否得到json字串Log.e("TAG", "" + sb.toString());
此json字串過大,導致buffer,申請記憶體失敗,


解決Android 記憶體溢出

其實你可以添加在你的清單

<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit;"><span class="atn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(230, 67, 32);"></span></code><pre class="default prettyprint prettyprinted" name="code" style="white-space: pre-wrap; word-wrap: normal; position: relative; overflow: auto; margin-top: 0px; margin-bottom: 1em; padding: 5px; border: 0px; width: auto; max-height: 600px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; background-color: rgb(239, 240, 241);"><code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit;"><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);"> </span><span class="atn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(230, 67, 32);">android:hardwareAccelerated</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">=</span><span class="atv" style="margin: 0px; padding: 0px; border: 0px; color: rgb(15, 116, 189);">"false"</span></code><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">,</span><pre class="default prettyprint prettyprinted" name="code" style="white-space: pre-wrap; word-wrap: normal; position: relative; overflow: auto; margin-top: 0px; margin-bottom: 1em; padding: 5px; border: 0px; width: auto; max-height: 600px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; background-color: rgb(239, 240, 241);"><code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit;"><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);"> </span><span class="atn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(230, 67, 32);">android:largeHeap</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">=</span><span class="atv" style="margin: 0px; padding: 0px; border: 0px; color: rgb(15, 116, 189);">"true"</span></code>
是工作的一些情況,
 
 

<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit;"><span class="tag" style="margin: 0px; padding: 0px; border: 0px; color: rgb(125, 39, 39);"><application</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">    </span><span class="atn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(230, 67, 32);">android:allowBackup</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">=</span><span class="atv" style="margin: 0px; padding: 0px; border: 0px; color: rgb(15, 116, 189);">"true"</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">    </span><span class="atn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(230, 67, 32);">android:hardwareAccelerated</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">=</span><span class="atv" style="margin: 0px; padding: 0px; border: 0px; color: rgb(15, 116, 189);">"false"</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">    </span><span class="atn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(230, 67, 32);">android:icon</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">=</span><span class="atv" style="margin: 0px; padding: 0px; border: 0px; color: rgb(15, 116, 189);">"@mipmap/ic_launcher"</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">    </span><span class="atn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(230, 67, 32);">android:label</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">=</span><span class="atv" style="margin: 0px; padding: 0px; border: 0px; color: rgb(15, 116, 189);">"@string/app_name"</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">    </span><span class="atn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(230, 67, 32);">android:largeHeap</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">=</span><span class="atv" style="margin: 0px; padding: 0px; border: 0px; color: rgb(15, 116, 189);">"true"</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">    </span><span class="atn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(230, 67, 32);">android:supportsRtl</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">=</span><span class="atv" style="margin: 0px; padding: 0px; border: 0px; color: rgb(15, 116, 189);">"true"</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">    </span><span class="atn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(230, 67, 32);">android:theme</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">=</span><span class="atv" style="margin: 0px; padding: 0px; border: 0px; color: rgb(15, 116, 189);">"@style/AppTheme"</span><span class="tag" style="margin: 0px; padding: 0px; border: 0px; color: rgb(125, 39, 39);">></span></code>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.