java.lang.OutOfMemoryError。 Android
java.lang.OutOfMemoryError. Android
我的应用程序开发还不算太远,我得到了 java.lang.OutOfMemoryError。这是 logcat:
02-24 14:02:54.957: E/art(28628): Throwing OutOfMemoryError "Failed to allocate a 28 byte allocation with 8 free bytes and 8B until OOM" (recursive case)
02-24 14:02:54.965: E/art(28628): "FinalizerDaemon" daemon prio=5 tid=8 Runnable
02-24 14:02:54.965: E/art(28628): | group="system" sCount=0 dsCount=0 obj=0x12c260e0 self=0xac4a5400
02-24 14:02:54.965: E/art(28628): | sysTid=28641 nice=0 cgrp=apps sched=0/0 handle=0xac4b9b00
02-24 14:02:54.965: E/art(28628): | state=R schedstat=( 2442370612 111306164 635 ) utm=229 stm=15 core=1 HZ=100
02-24 14:02:54.965: E/art(28628): | stack=0xb40fe000-0xb4100000 stackSize=1036KB
02-24 14:02:54.965: E/art(28628): | held mutexes= "mutator lock"(shared held)
02-24 14:02:54.965: E/art(28628): at com.android.internal.os.BinderInternal$GcWatcher.finalize(BinderInternal.java:51)
02-24 14:02:54.965: E/art(28628): at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:190)
02-24 14:02:54.965: E/art(28628): at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:173)
02-24 14:02:54.965: E/art(28628): at java.lang.Thread.run(Thread.java:818)
02-24 14:02:54.965: E/System(28628): Uncaught exception thrown by finalizer
我正在尝试制作一个列表,但每当我检索数据以制作列表时,我都会收到错误消息。
这里是 class 扩展列表片段
`
public class BillReminderListFragment extends ListFragment {
private ArrayList<Bill> mBills;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
mBills = BillLab.get(getActivity()).getBills(); <---(this is what is causing the error)
}
}
这里是保存数据的class
public class BillLab {
private ArrayList<Bill> mBills;
private static BillLab sBillLab;
private Context mAppBillContext;
private BillLab(Context appBillContext){
mAppBillContext = appBillContext;
mBills = new ArrayList<Bill>();
for (int i = 0; 1 < 100; i ++){
Bill b = new Bill();
b.setTitle("Bill #" + i);
b.setDate(new Date());
mBills.add(b);
}
}
public static BillLab get(Context c){
if (sBillLab == null){
sBillLab = new BillLab(c.getApplicationContext());
}
return sBillLab;
}
public Bill getBill(UUID id){
for (Bill b: mBills) {
if (b.getId().equals(id))
return b;
}
return null;
}
public ArrayList<Bill> getBills(){
return mBills;
}
}
请帮忙。这是我大学的最后一个项目,我没有太多时间。
谢谢。
`
你的 for 循环有错别字:
for (int i = 0; 1 < 100; i ++)
应该是i < 100
。 运行 内存不足,因为 1 总是小于 100。
更改下一行
for (int i = 0; 1 < 100; i ++){
至
for (int i = 0; i < 100; i ++){
这个方法有两个根本原因,一个是@Nag edi 提到的,但是 made java.lang.OutOfMemoryError 的根本原因是:-
private BillLab(Context appBillContext){
mAppBillContext = appBillContext;
mBills = new ArrayList<Bill>();
for (int i = 0; 1 < 100; i ++){ // this line will cause the infinity loop only
Bill b = new Bill(); // this is the root cause to create every single object for each loop.
b.setTitle("Bill #" + i);
b.setDate(new Date());
mBills.add(b);
}
}
至 :-
private BillLab(Context appBillContext){
mAppBillContext = appBillContext;
mBills = new ArrayList<Bill>();
Bill b = null;
for (int i = 0; i < 100; i ++){
b = new Bill();
b.setTitle("Bill #" + i);
b.setDate(new Date());
mBills.add(b);
}
}
我的应用程序开发还不算太远,我得到了 java.lang.OutOfMemoryError。这是 logcat:
02-24 14:02:54.957: E/art(28628): Throwing OutOfMemoryError "Failed to allocate a 28 byte allocation with 8 free bytes and 8B until OOM" (recursive case)
02-24 14:02:54.965: E/art(28628): "FinalizerDaemon" daemon prio=5 tid=8 Runnable
02-24 14:02:54.965: E/art(28628): | group="system" sCount=0 dsCount=0 obj=0x12c260e0 self=0xac4a5400
02-24 14:02:54.965: E/art(28628): | sysTid=28641 nice=0 cgrp=apps sched=0/0 handle=0xac4b9b00
02-24 14:02:54.965: E/art(28628): | state=R schedstat=( 2442370612 111306164 635 ) utm=229 stm=15 core=1 HZ=100
02-24 14:02:54.965: E/art(28628): | stack=0xb40fe000-0xb4100000 stackSize=1036KB
02-24 14:02:54.965: E/art(28628): | held mutexes= "mutator lock"(shared held)
02-24 14:02:54.965: E/art(28628): at com.android.internal.os.BinderInternal$GcWatcher.finalize(BinderInternal.java:51)
02-24 14:02:54.965: E/art(28628): at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:190)
02-24 14:02:54.965: E/art(28628): at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:173)
02-24 14:02:54.965: E/art(28628): at java.lang.Thread.run(Thread.java:818)
02-24 14:02:54.965: E/System(28628): Uncaught exception thrown by finalizer
我正在尝试制作一个列表,但每当我检索数据以制作列表时,我都会收到错误消息。
这里是 class 扩展列表片段
`
public class BillReminderListFragment extends ListFragment {
private ArrayList<Bill> mBills;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
mBills = BillLab.get(getActivity()).getBills(); <---(this is what is causing the error)
}
}
这里是保存数据的class
public class BillLab {
private ArrayList<Bill> mBills;
private static BillLab sBillLab;
private Context mAppBillContext;
private BillLab(Context appBillContext){
mAppBillContext = appBillContext;
mBills = new ArrayList<Bill>();
for (int i = 0; 1 < 100; i ++){
Bill b = new Bill();
b.setTitle("Bill #" + i);
b.setDate(new Date());
mBills.add(b);
}
}
public static BillLab get(Context c){
if (sBillLab == null){
sBillLab = new BillLab(c.getApplicationContext());
}
return sBillLab;
}
public Bill getBill(UUID id){
for (Bill b: mBills) {
if (b.getId().equals(id))
return b;
}
return null;
}
public ArrayList<Bill> getBills(){
return mBills;
}
}
请帮忙。这是我大学的最后一个项目,我没有太多时间。 谢谢。
`
你的 for 循环有错别字:
for (int i = 0; 1 < 100; i ++)
应该是i < 100
。 运行 内存不足,因为 1 总是小于 100。
更改下一行
for (int i = 0; 1 < 100; i ++){
至
for (int i = 0; i < 100; i ++){
这个方法有两个根本原因,一个是@Nag edi 提到的,但是 made java.lang.OutOfMemoryError 的根本原因是:-
private BillLab(Context appBillContext){
mAppBillContext = appBillContext;
mBills = new ArrayList<Bill>();
for (int i = 0; 1 < 100; i ++){ // this line will cause the infinity loop only
Bill b = new Bill(); // this is the root cause to create every single object for each loop.
b.setTitle("Bill #" + i);
b.setDate(new Date());
mBills.add(b);
}
}
至 :-
private BillLab(Context appBillContext){
mAppBillContext = appBillContext;
mBills = new ArrayList<Bill>();
Bill b = null;
for (int i = 0; i < 100; i ++){
b = new Bill();
b.setTitle("Bill #" + i);
b.setDate(new Date());
mBills.add(b);
}
}