Android-ArrayList 内存不足异常<Sting>
Android-Out Of Memory Exception For ArrayList<Sting>
我的代码 运行 在我设置一些 if else 条件之前很好。
我认为 for 循环中存在优化问题但没有得到它
btSetQuantity.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (etbillQuantity.getText().toString().equals("")) {
Toast.makeText(getApplicationContext(),
"Quantity Not Entered", Toast.LENGTH_SHORT)
.show();
InputMethodManager imm = (InputMethodManager) getSystemService(context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(
etbillQuantity.getWindowToken(), 0);
alertDialog.dismiss();
} else if (Double.parseDouble(etbillQuantity.getText()
.toString()) > initialQuantityValue) {
Toast.makeText(getApplicationContext(),
"Short Of Quantity", Toast.LENGTH_SHORT)
.show();
} else {
strEnteredQuantity = etbillQuantity.getText()
.toString();
enteredQuantityValue = Double
.parseDouble(strEnteredQuantity);
tempproduct_TotalQuantity.add(tvInitialQuantity
.getText().toString());
newQuantityValue = initialQuantityValue
- enteredQuantityValue;
setQuantity.setText(strEnteredQuantity);
String setQuantityStr = "";
GlobalVariables.arrayOfTempProductSetQuantity
.add(strEnteredQuantity);
GlobalVariables.arrayOfTempProductName.add(tvName
.getText().toString());
GlobalVariables.arrayOfTempProductCode.add(tvCode
.getText().toString());
GlobalVariables.arrayOfTempProductUnitPrice
.add(tvPrice.getText().toString());
GlobalVariables.arrayOfTempProductQuantity
.add((String.valueOf(newQuantityValue)));
if (tempproduct_Code.size() <= 0) {
tempproduct_Code.add(tvCode.getText()
.toString());
tempproduct_Name.add(tvName.getText()
.toString());
tempproduct_Quantity.add(strEnteredQuantity);
tempproduct_UnitPrice.add(tvPrice.getText()
.toString());
}
else {
String setQuantityTemp = "";
String setUnitPriceTemp = "";
String setNameTemp = "";
String setCodeTemp = "";
for (int i = 0; i < (tempproduct_Code.size()); i++) {
for (int j = 0; j < (GlobalVariables.arrayOfTempProductCode
.size()); j++) {
if (tempproduct_Code
.get(i)
.contains(
GlobalVariables.arrayOfTempProductCode
.get(j))) {
setQuantityTemp = GlobalVariables.arrayOfTempProductSetQuantity
.get(j);
tempproduct_Quantity.set(i,
setQuantityTemp);
setCodeTemp = GlobalVariables.arrayOfTempProductCode
.get(j);
tempproduct_Code
.set(i, setCodeTemp);
setNameTemp = GlobalVariables.arrayOfTempProductName
.get(j);
tempproduct_Name
.set(i, setNameTemp);
setUnitPriceTemp = GlobalVariables.arrayOfTempProductUnitPrice
.get(j);
tempproduct_UnitPrice.set(i,
setUnitPriceTemp);
} else {
tempproduct_Code.add(tvCode
.getText().toString());
tempproduct_Name.add(tvName
.getText().toString());
tempproduct_Quantity
.add(strEnteredQuantity);
tempproduct_UnitPrice.add(tvPrice
.getText().toString());
}
}
}
}
InputMethodManager imm = (InputMethodManager) getSystemService(context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(
etbillQuantity.getWindowToken(), 0);
onSetQuantityDisplayData();
alertDialog.dismiss();
}
}
});
它现在在日志中显示内存不足
我已经使用了列表视图性能优化的最佳方法
日志显示数组列表内存不足
日志还显示堆增长了一段时间应用程序是结构和崩溃
04-24 12:31:27.463: I/dalvikvm-heap(1136): Grow heap (frag case) to 20.725MB for 4021256-byte allocation
04-24 12:31:28.063: D/dalvikvm(1136): GC_FOR_ALLOC freed 2618K, 34% free 18530K/27816K, paused 536ms, total 536ms
04-24 12:31:28.063: I/dalvikvm-heap(1136): Grow heap (frag case) to 22.003MB for 4021256-byte allocation
04-24 12:31:37.813: D/dalvikvm(1136): GC_FOR_ALLOC freed 2618K, 29% free 19839K/27816K, paused 708ms, total 708ms
04-24 12:31:37.813: I/dalvikvm-heap(1136): Forcing collection of SoftReferences for 6031876-byte allocation
04-24 12:31:38.553: D/dalvikvm(1136): GC_BEFORE_OOM freed 9K, 29% free 19829K/27816K, paused 730ms, total 732ms
04-24 12:31:38.553: E/dalvikvm-heap(1136): Out of memory on a 6031876-byte allocation.
04-24 08:56:04.143: E/AndroidRuntime(2853): FATAL EXCEPTION: main
04-24 08:56:04.143: E/AndroidRuntime(2853): Process: com.vardhan.android.navigationdrawerexample, PID: 2853
04-24 08:56:04.143: E/AndroidRuntime(2853): java.lang.OutOfMemoryError
04-24 08:56:04.143: E/AndroidRuntime(2853): at java.util.ArrayList.add(ArrayList.java:118)
04-24 08:56:04.143: E/AndroidRuntime(2853): at com.vardhan.android.navigationdrawerexample.BillingProductListView.onClick(BillingProductListView.java:349)
04-24 08:56:04.143: E/AndroidRuntime(2853): at android.view.View.performClick(View.java:4438)
04-24 08:56:04.143: E/AndroidRuntime(2853): at android.view.View$PerformClick.run(View.java:18422)
04-24 08:56:04.143: E/AndroidRuntime(2853): at android.os.Handler.handleCallback(Handler.java:733)
04-24 08:56:04.143: E/AndroidRuntime(2853): at android.os.Handler.dispatchMessage(Handler.java:95)
04-24 08:56:04.143: E/AndroidRuntime(2853): at android.os.Looper.loop(Looper.java:136)
04-24 08:56:04.143: E/AndroidRuntime(2853): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-24 08:56:04.143: E/AndroidRuntime(2853): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 08:56:04.143: E/AndroidRuntime(2853): at java.lang.reflect.Method.invoke(Method.java:515)
04-24 08:56:04.143: E/AndroidRuntime(2853): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-24 08:56:04.143: E/AndroidRuntime(2853): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-24 08:56:04.143: E/AndroidRuntime(2853): at dalvik.system.NativeStart.main(Native Method)
看到这里,由于你的逻辑错误,外层循环变成了无限循环。
else {
// YOUR OTHER CODES
for (int i = 0; i < (tempproduct_Code.size()); i++) {
for (int j = 0; j < (GlobalVariables.arrayOfTempProductCode .size()); j++) {
if (tempproduct_Code.get(i).contains(GlobalVariables.arrayOfTempProductCode
.get(j))) {
// your other codes
} else {
tempproduct_Code.add(tvCode.getText().toString()); // here you are continuously increasing your tempproduct_Code's size and your outer loops condition is also depending on the same size. thus your loop is running & ultimately leading you to OutOfMemory error
tempproduct_Name.add(tvName.getText().toString());
tempproduct_Quantity.add(strEnteredQuantity);
tempproduct_UnitPrice.add(tvPrice.getText().toString());
}
}
}
所以你的代码变得像一只狗在追自己的尾巴。检查你的业务需求并修改你的逻辑
我的代码 运行 在我设置一些 if else 条件之前很好。 我认为 for 循环中存在优化问题但没有得到它
btSetQuantity.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (etbillQuantity.getText().toString().equals("")) {
Toast.makeText(getApplicationContext(),
"Quantity Not Entered", Toast.LENGTH_SHORT)
.show();
InputMethodManager imm = (InputMethodManager) getSystemService(context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(
etbillQuantity.getWindowToken(), 0);
alertDialog.dismiss();
} else if (Double.parseDouble(etbillQuantity.getText()
.toString()) > initialQuantityValue) {
Toast.makeText(getApplicationContext(),
"Short Of Quantity", Toast.LENGTH_SHORT)
.show();
} else {
strEnteredQuantity = etbillQuantity.getText()
.toString();
enteredQuantityValue = Double
.parseDouble(strEnteredQuantity);
tempproduct_TotalQuantity.add(tvInitialQuantity
.getText().toString());
newQuantityValue = initialQuantityValue
- enteredQuantityValue;
setQuantity.setText(strEnteredQuantity);
String setQuantityStr = "";
GlobalVariables.arrayOfTempProductSetQuantity
.add(strEnteredQuantity);
GlobalVariables.arrayOfTempProductName.add(tvName
.getText().toString());
GlobalVariables.arrayOfTempProductCode.add(tvCode
.getText().toString());
GlobalVariables.arrayOfTempProductUnitPrice
.add(tvPrice.getText().toString());
GlobalVariables.arrayOfTempProductQuantity
.add((String.valueOf(newQuantityValue)));
if (tempproduct_Code.size() <= 0) {
tempproduct_Code.add(tvCode.getText()
.toString());
tempproduct_Name.add(tvName.getText()
.toString());
tempproduct_Quantity.add(strEnteredQuantity);
tempproduct_UnitPrice.add(tvPrice.getText()
.toString());
}
else {
String setQuantityTemp = "";
String setUnitPriceTemp = "";
String setNameTemp = "";
String setCodeTemp = "";
for (int i = 0; i < (tempproduct_Code.size()); i++) {
for (int j = 0; j < (GlobalVariables.arrayOfTempProductCode
.size()); j++) {
if (tempproduct_Code
.get(i)
.contains(
GlobalVariables.arrayOfTempProductCode
.get(j))) {
setQuantityTemp = GlobalVariables.arrayOfTempProductSetQuantity
.get(j);
tempproduct_Quantity.set(i,
setQuantityTemp);
setCodeTemp = GlobalVariables.arrayOfTempProductCode
.get(j);
tempproduct_Code
.set(i, setCodeTemp);
setNameTemp = GlobalVariables.arrayOfTempProductName
.get(j);
tempproduct_Name
.set(i, setNameTemp);
setUnitPriceTemp = GlobalVariables.arrayOfTempProductUnitPrice
.get(j);
tempproduct_UnitPrice.set(i,
setUnitPriceTemp);
} else {
tempproduct_Code.add(tvCode
.getText().toString());
tempproduct_Name.add(tvName
.getText().toString());
tempproduct_Quantity
.add(strEnteredQuantity);
tempproduct_UnitPrice.add(tvPrice
.getText().toString());
}
}
}
}
InputMethodManager imm = (InputMethodManager) getSystemService(context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(
etbillQuantity.getWindowToken(), 0);
onSetQuantityDisplayData();
alertDialog.dismiss();
}
}
});
它现在在日志中显示内存不足 我已经使用了列表视图性能优化的最佳方法 日志显示数组列表内存不足 日志还显示堆增长了一段时间应用程序是结构和崩溃
04-24 12:31:27.463: I/dalvikvm-heap(1136): Grow heap (frag case) to 20.725MB for 4021256-byte allocation
04-24 12:31:28.063: D/dalvikvm(1136): GC_FOR_ALLOC freed 2618K, 34% free 18530K/27816K, paused 536ms, total 536ms
04-24 12:31:28.063: I/dalvikvm-heap(1136): Grow heap (frag case) to 22.003MB for 4021256-byte allocation
04-24 12:31:37.813: D/dalvikvm(1136): GC_FOR_ALLOC freed 2618K, 29% free 19839K/27816K, paused 708ms, total 708ms
04-24 12:31:37.813: I/dalvikvm-heap(1136): Forcing collection of SoftReferences for 6031876-byte allocation
04-24 12:31:38.553: D/dalvikvm(1136): GC_BEFORE_OOM freed 9K, 29% free 19829K/27816K, paused 730ms, total 732ms
04-24 12:31:38.553: E/dalvikvm-heap(1136): Out of memory on a 6031876-byte allocation.
04-24 08:56:04.143: E/AndroidRuntime(2853): FATAL EXCEPTION: main
04-24 08:56:04.143: E/AndroidRuntime(2853): Process: com.vardhan.android.navigationdrawerexample, PID: 2853
04-24 08:56:04.143: E/AndroidRuntime(2853): java.lang.OutOfMemoryError
04-24 08:56:04.143: E/AndroidRuntime(2853): at java.util.ArrayList.add(ArrayList.java:118)
04-24 08:56:04.143: E/AndroidRuntime(2853): at com.vardhan.android.navigationdrawerexample.BillingProductListView.onClick(BillingProductListView.java:349)
04-24 08:56:04.143: E/AndroidRuntime(2853): at android.view.View.performClick(View.java:4438)
04-24 08:56:04.143: E/AndroidRuntime(2853): at android.view.View$PerformClick.run(View.java:18422)
04-24 08:56:04.143: E/AndroidRuntime(2853): at android.os.Handler.handleCallback(Handler.java:733)
04-24 08:56:04.143: E/AndroidRuntime(2853): at android.os.Handler.dispatchMessage(Handler.java:95)
04-24 08:56:04.143: E/AndroidRuntime(2853): at android.os.Looper.loop(Looper.java:136)
04-24 08:56:04.143: E/AndroidRuntime(2853): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-24 08:56:04.143: E/AndroidRuntime(2853): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 08:56:04.143: E/AndroidRuntime(2853): at java.lang.reflect.Method.invoke(Method.java:515)
04-24 08:56:04.143: E/AndroidRuntime(2853): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-24 08:56:04.143: E/AndroidRuntime(2853): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-24 08:56:04.143: E/AndroidRuntime(2853): at dalvik.system.NativeStart.main(Native Method)
看到这里,由于你的逻辑错误,外层循环变成了无限循环。
else {
// YOUR OTHER CODES
for (int i = 0; i < (tempproduct_Code.size()); i++) {
for (int j = 0; j < (GlobalVariables.arrayOfTempProductCode .size()); j++) {
if (tempproduct_Code.get(i).contains(GlobalVariables.arrayOfTempProductCode
.get(j))) {
// your other codes
} else {
tempproduct_Code.add(tvCode.getText().toString()); // here you are continuously increasing your tempproduct_Code's size and your outer loops condition is also depending on the same size. thus your loop is running & ultimately leading you to OutOfMemory error
tempproduct_Name.add(tvName.getText().toString());
tempproduct_Quantity.add(strEnteredQuantity);
tempproduct_UnitPrice.add(tvPrice.getText().toString());
}
}
}
所以你的代码变得像一只狗在追自己的尾巴。检查你的业务需求并修改你的逻辑