我有一个应用程序,其中需要一个大型 SQLite 数据库,现在当我的模拟器的堆大小为 16 MiB 时向表中添加行时,它崩溃说内存不足,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
这是我的字符串生成器所在的位置,这导致了问题:
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();
}
}
}