[英]Java - Memory Allocation Difficulties (GC_FOR_ALLOC)
My Android app crash when I start this Thread. 当我启动此线程时,我的Android应用程序崩溃。
This Thread should restart the phone. 这个线程应该重启手机。 When I start it it don't restart the phone and I have the following text in the LOG :
当我启动它时,它不会重新启动手机,我在LOG中有以下文字:
08-25 09:12:00.946 26029-26813/com.datasulting.chris.smsgateway D/dalvikvm: GC_FOR_ALLOC freed 1279K (30823), 55% free 4485K/9968K, paused 53ms, total 53ms 08-25 09:12:01.294 26029-26813/com.datasulting.chris.smsgateway D/dalvikvm: between the previous GC alloc 1280K 08-25 09:12:01.346 26029-26813/com.datasulting.chris.smsgateway D/dalvikvm: GC_FOR_ALLOC freed 1280K (30820), 55% free 4485K/9968K, paused 52ms, total 52ms 08-25 09:12:01.713 26029-26813/com.datasulting.chris.smsgateway D/dalvikvm: between the previous GC alloc 1279K 08-25 09:12:01.768 26029-26813/com.datasulting.chris.smsgateway D/dalvikvm: GC_FOR_ALLOC freed 1279K (30813), 55% free 4486K/9968K, paused 55ms, total 55ms 08-25 09:12:02.111 26029-26813/com.datasulting.chris.smsgateway D/dalvikvm: between the previous GC alloc 1279K 08-25 09:12:02.164 26029-26813/com.datasulting.chris.smsgateway D/dalvikvm: GC_FOR_ALLOC freed 1280K (30819), 55% free 4486K/9968K, paused 53ms, total 53ms 08-25 09:12:02.504 26029-26813/com.datas
08-25 09:12:00.946 26029-26813 / com.datasulting.chris.smsgateway D / dalvikvm:GC_FOR_ALLOC释放1279K(30823),55%免费4485K / 9968K,暂停53ms,总计53ms 08-25 09:12:01.294 26029-26813 / com.datasulting.chris.smsgateway D / dalvikvm:在之前的GC之间分配1280K 08-25 09:12:01.346 26029-26813 / com.datasulting.chris.smsgateway D / dalvikvm:GC_FOR_ALLOC释放1280K(30820) ,55%免费4485K / 9968K,暂停52ms,总计52ms 08-25 09:12:01.713 26029-26813 / com.datasulting.chris.smsgateway D / dalvikvm:在之前的GC之间分配1279K 08-25 09:12:01.768 26029-26813 / com.datasulting.chris.smsgateway D / dalvikvm:GC_FOR_ALLOC释放1279K(30813),55%免费4486K / 9968K,暂停55ms,总计55ms 08-25 09:12:02.111 26029-26813 / com.datasulting。 chris.smsgateway D / dalvikvm:在之前的GC之间分配1279K 08-25 09:12:02.164 26029-26813 / com.datasulting.chris.smsgateway D / dalvikvm:GC_FOR_ALLOC释放1280K(30819),55%免费4486K / 9968K,暂停53ms,总计53ms 08-25 09:12:02.504 26029-26813 / com.datas ulting.chris.smsgateway D/dalvikvm: between the previous GC alloc 1279K 08-25 09:12:02.557 26029-26813/com.datasulting.chris.smsgateway D/dalvikvm: GC_FOR_ALLOC freed 1280K (30823), 55% free 4485K/9968K, paused 53ms, total 53ms 08-25 09:12:02.901 26029-26813/com.datasulting.chris.smsgateway D/dalvikvm: between the previous GC alloc 1279K 08-25 09:12:02.956 26029-26813/com.datasulting.chris.smsgateway D/dalvikvm: GC_FOR_ALLOC freed 1279K (30818), 55% free 4485K/9968K, paused 55ms, total 55ms 08-25 09:12:03.298 26029-26813/com.datasulting.chris.smsgateway D/dalvikvm: between the previous GC alloc 1280K
ulting.chris.smsgateway D / dalvikvm:在之前的GC之间分配1279K 08-25 09:12:02.557 26029-26813 / com.datasulting.chris.smsgateway D / dalvikvm:GC_FOR_ALLOC释放1280K(30823),55%免费4485K / 9968K,暂停53ms,总计53ms 08-25 09:12:02.901 26029-26813 / com.datasulting.chris.smsgateway D / dalvikvm:在之前的GC alloc 1279K 08-25 09:12:02.956 26029-26813 / com之间。 datasulting.chris.smsgateway D / dalvikvm:GC_FOR_ALLOC释放1279K(30818),55%免费4485K / 9968K,暂停55ms,总计55ms 08-25 09:12:03.298 26029-26813 / com.datasulting.chris.smsgateway D / dalvikvm :之前的GC之间分配1280K
This is my Thread. 这是我的主题。
class Reboot implements Runnable {
private volatile boolean cancelled;
Boolean checkRebootHeb;
Boolean checkRebootQuo;
int jourDemandeeInt;
String jourDemandeeString;
String weekDay;
int dayOfWeek;
SimpleDateFormat df;
String heure;
String dayOfWeekString;
String heureDemandee;
Calendar c;
public Reboot(Boolean VARcheckReboot, Boolean VARcheckRebootQuo, int VARjour, String VARtextReboot) {
checkRebootHeb = VARcheckReboot;
checkRebootQuo = VARcheckRebootQuo;
jourDemandeeInt = VARjour;
heureDemandee = VARtextReboot;
}
@Override
public void run() {
while (!cancelled) {
if (jourDemandeeInt == 0){
jourDemandeeString = "Lundi";
}
if (jourDemandeeInt == 1){
jourDemandeeString = "Mardi";
}
if (jourDemandeeInt == 2){
jourDemandeeString = "Mercredi";
}
if (jourDemandeeInt == 3){
jourDemandeeString = "Jeudi";
}
if (jourDemandeeInt == 4){
jourDemandeeString = "Vendredi";
}
if (jourDemandeeInt == 5){
jourDemandeeString = "Samedi";
}
if (jourDemandeeInt == 6){
jourDemandeeString = "Dimanche";
}
c = Calendar.getInstance();
dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
df = new SimpleDateFormat("HH:mm");
heure = df.format(c.getTime());
if (Calendar.MONDAY == dayOfWeek) weekDay = "Lundi";
else if (Calendar.TUESDAY == dayOfWeek) weekDay = "Mardi";
else if (Calendar.WEDNESDAY == dayOfWeek) weekDay = "Mercredi";
else if (Calendar.THURSDAY == dayOfWeek) weekDay = "Jeudi";
else if (Calendar.FRIDAY == dayOfWeek) weekDay = "Vendredi";
else if (Calendar.SATURDAY == dayOfWeek) weekDay = "Samedi";
else if (Calendar.SUNDAY == dayOfWeek) weekDay = "Dimanche";
dayOfWeekString = String.valueOf(dayOfWeek);
if (checkRebootQuo == true) {
if (heure.equals(heureDemandee)) {
try {
Process proc = Runtime.getRuntime().exec(new String[]{"su", "-c", "reboot"});
proc.waitFor();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
if (checkRebootHeb == true) {
if (dayOfWeekString.equals(jourDemandeeString)) {
if (heure.equals(heureDemandee)) {
try {
Process proc = Runtime.getRuntime().exec(new String[]{"su", "-c", "reboot"});
proc.waitFor();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
}
}
public void cancel() {
cancelled = true;
}
}
Lots of things wrong with that code. 这段代码有很多问题。
Your main problem - your thread is doing "active" waiting. 你的主要问题 - 你的线程在做“主动”等待。 This means: it just loops and creates a new Calendar object each iteration .
这意味着:它只是循环并在每次迭代时创建一个新的Calendar对象。 Then you immediately throw away that object, and create a new one.
然后你立即扔掉那个物体,并创造一个新物体。
And you are surprised that the garbage collector has a hard time with your code? 而且你很惊讶垃圾收集器很难用你的代码? And just to be sure that my sarcasm doesn't prevent people from understanding the issue: no garbage collector in the world is designed to allow for a "hot" loop that only creates garbage objects;
并且只是为了确保我的讽刺并不妨碍人们理解这个问题:世界上没有垃圾收集器被设计为允许仅产生垃圾对象的“热”循环; especially in the "mobile" world.
特别是在“移动”世界。
So, the obvious answer: add some Thread.sleep() statement in the body of your loop; 所以,明显的答案是:在循环体中添加一些Thread.sleep()语句; like:
喜欢:
Your code does: - check if its the hour to reboot - if yes: reboot - repeat 你的代码确实: - 检查它是否需要重启的时间 - 如果是:重启 - 重复
Then, some general feedback on your (sorry) horrible code: 然后,对你(遗憾的)可怕代码的一些一般性反馈:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.