我有一个应用程序需要一个大的SQLite数据库,现在我在我的模拟器的堆大小为16 MiB时向表中添加了行,它崩溃了说outofmemory,32 MiB工作正常。我使用了一个用于GET的字符串构建器。
那么我现在该怎么办?有没有办法限制我的应用程序在某些堆大小的某些手机上使用?如果特别这样,怎么办呢?
先谢谢,这是我的错误代码:
09-02 13:21:08.805: E/AndroidRuntime(482): FATAL EXCEPTION: AsyncTask #1
09-02 13:21:08.805: E/AndroidRuntime(482): java.lang.RuntimeException: An error occured while executing doInBackground()
09-02 13:21:08.805: E/AndroidRuntime(482): at android.os.AsyncTask$3.done(AsyncTask.java:200)
09-02 13:21:08.805: E/AndroidRuntime(482): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
09-02 13:21:08.805: E/AndroidRuntime(482): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
09-02 13:21:08.805: E/AndroidRuntime(482): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
09-02 13:21:08.805: E/AndroidRuntime(482): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-02 13:21:08.805: E/AndroidRuntime(482): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
09-02 13:21:08.805: E/AndroidRuntime(482): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
09-02 13:21:08.805: E/AndroidRuntime(482): at java.lang.Thread.run(Thread.java:1096)
09-02 13:21:08.805: E/AndroidRuntime(482): Caused by: java.lang.OutOfMemoryError
09-02 13:21:08.805: E/AndroidRuntime(482): at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:97)
09-02 13:21:08.805: E/AndroidRuntime(482): at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:136)
09-02 13:21:08.805: E/AndroidRuntime(482): at java.lang.StringBuilder.append(StringBuilder.java:272)
09-02 13:21:08.805: E/AndroidRuntime(482): at java.io.BufferedReader.readLine(BufferedReader.java:452)
09-02 13:21:08.805: E/AndroidRuntime(482): at com.ex.nok.Src.prodGet(Src.java:157)
09-02 13:21:08.805: E/AndroidRuntime(482): at com.ex.nok.MainActivity$RestRequ.doInBackground(MainActivity.java:398)
09-02 13:21:08.805: E/AndroidRuntime(482): at com.ex.nok.MainActivity$RestRequ.doInBackground(MainActivity.java:1)
09-02 13:21:08.805: E/AndroidRuntime(482): at android.os.AsyncTask$2.call(AsyncTask.java:185)
09-02 13:21:08.805: E/AndroidRuntime(482): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-02 13:21:08.805: E/AndroidRuntime(482): ... 4 more
这是我的stringbuilder所在的位置,这会导致问题:
public static void prodGet (Activity act, String urlStr) {
int productId;
String productName;
UrunSQLiteHelper dbProds = new UrunSQLiteHelper(act);
HttpURLConnection conn = null;
BufferedReader rd = null;
try {
URL url = new URL(urlStr);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(6000);
conn.setReadTimeout(10000);
if (conn.getResponseCode() >= 200 && conn.getResponseCode() < 300) {
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) { //<<<<<<
if (line != null || line.length() != 0) {
JSONTokener prodTokener = new JSONTokener(line);
JSONArray prodArray = new JSONArray(prodTokener);
for(int h=0; h<(prodArray.length()); h++)
{
JSONObject json_obj_prd = prodArray.getJSONObject(h);
productId = json_obj_prd.getString("PRODID");
productName = json_obj_prd.getString("PRODNAME");
dbProds.addProduct(new Product(productId, productName));
}
dbProds.close();
}
}
rd.close();
conn.disconnect();
}
else {
}
}
catch (Exception e) {
Log.e("APP", "exception", e);
}
finally {
if (rd != null) {
try {
rd.close();
} catch (IOException e) {
}
}
if (conn != null) {
conn.disconnect();
}
}
}