How to reset android service memory

2019-08-24 10:51发布

问题:

The issue is I am restarting my service and my memory is still tied up. This is my back button at my main activity. I call the stopsevice startservice method

@Override
public void onBackPressed() {
    stopService(new Intent(this, AUTOAlarmService.class));
    startService(new Intent(this, AUTOAlarmService.class));
    finish();
}

-- The service restarts but the memory doesn't reset. The service is sitting on 50MB of memory and has been running for a couple hours. The method resets it but doesn't free up the memory. Is there some obscure method I'm missing that you can use to free up that memory?

public class AUTOAlarmService extends Service {
    public static final String DEFAULTNAME = "DefaultFile";
    Random ran = new Random(50000);
    ImageButton iblAUTOMATIONITEM, ibAUTOSEARCHSETTINGS, ibFINSHAUTOITEM;
    Button ibAUTOLOCATION, ibAUTOCATEGORY;
    EditText etNAME, etKEYWORDS;
    int numz;
    static final int uniqueID[] = { 1985, 1986, 1987, 1988, 1989, 1990, 1991,
            1992, 1993, 1994, 2000 };
    StringBuffer NPOCHECKING = new StringBuffer();
    private PowerManager.WakeLock mWakeLock0, mWakeLock1, mWakeLock2,
            mWakeLock3, mWakeLock4, mWakeLock5, mWakeLock6, mWakeLock7,
            mWakeLock8, mWakeLock9;
String THELocation = null;
String Checkn = null;
String THESEARCHSTRING = null;
String SearchKEYWORDS;
String THEName = null;
String KeyFront = "&query=";
String me = "NEWAUTO";
String SET = null;
String RUNNER = "AUTORUNNER-1";

@Override
public void onCreate() {
}

@Override
public IBinder onBind(Intent intent) {
    return null;`enter code here`
}



@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    SharedPreferences Search = getSharedPreferences(DEFAULTNAME, 0);
    String C0 = Search.getString("NICKCHECKER0", "none");
    String C1 = Search.getString("NICKCHECKER1", "none");
    String C2 = Search.getString("NICKCHECKER2", "none");
    String C3 = Search.getString("NICKCHECKER3", "none");
    String C4 = Search.getString("NICKCHECKER4", "none");
    String C5 = Search.getString("NICKCHECKER5", "none");
    String C6 = Search.getString("NICKCHECKER6", "none");
    String C7 = Search.getString("NICKCHECKER7", "none");
    String C8 = Search.getString("NICKCHECKER8", "none");
    String C9 = Search.getString("NICKCHECKER9", "none");
    String me0 = Search.getString("NickT0", "XJASZZZZZZZZZZZ");
    String me1 = Search.getString("NickT1", "XJASZZZZZZZZZZZ");
    String me2 = Search.getString("NickT2", "XJASZZZZZZZZZZZ");
    String me3 = Search.getString("NickT3", "XJASZZZZZZZZZZZ");
    String me4 = Search.getString("NickT4", "XJASZZZZZZZZZZZ");
    String me5 = Search.getString("NickT5", "XJASZZZZZZZZZZZ");
    String me6 = Search.getString("NickT6", "XJASZZZZZZZZZZZ");
    String me7 = Search.getString("NickT7", "XJASZZZZZZZZZZZ");
    String me8 = Search.getString("NickT8", "XJASZZZZZZZZZZZ");
    String me9 = Search.getString("NickT9", "XJASZZZZZZZZZZZ");
    Map<Thread, StackTraceElement[]> mee = Thread.getAllStackTraces();
    String C = mee.toString();
    if (C0.contentEquals("me0") == true && C.contains(me0) != true) {
        NICKX0();
        Sleeper();
    }
    if (C1.contentEquals("me1") == true && C.contains(me1) != true) {
        NICKX1();
        Sleeper();
    }
    if (C2.contentEquals("me2") == true && C.contains(me2) != true) {
        NICKX2();
        Sleeper();
    }
    if (C3.contentEquals("me3") == true && C.contains(me3) != true) {
        NICKX3();
        Sleeper();
    }
    if (C4.contentEquals("me4") == true && C.contains(me4) != true) {
        NICKX4();
        Sleeper();
    }
    if (C5.contentEquals("me5") == true && C.contains(me5) != true) {
        NICKX5();
        Sleeper();
    }
    if (C6.contentEquals("me6") == true && C.contains(me6) != true) {
        NICKX6();
        Sleeper();
    }
    if (C7.contentEquals("me7") == true && C.contains(me7) != true) {
        NICKX7();
        Sleeper();
    }
    if (C8.contentEquals("me8") == true && C.contains(me8) != true) {
        NICKX8();
        Sleeper();
    }
    if (C9.contentEquals("me9") == true && C.contains(me9) != true) {
        NICKX9();
        Sleeper();
    }
    return START_NOT_STICKY;
}

-- So these threads check servers for data and display a notification if the data is something the user hasn't viewed. They run until a users selects to turn them off. Should I kill these threads when I call stopservice?

private void NICKX0() {
        Thread AUTOMATIONTIMER = new Thread(new Runnable() {
            @SuppressLint("Wakelock")
            @SuppressWarnings({ "rawtypes", "deprecation" })
            @Override
            public void run() {
                PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
                mWakeLock0 = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                        "Wake Log");
                SharedPreferences Search = getSharedPreferences(DEFAULTNAME, 0);
                SharedPreferences.Editor SearchE = Search.edit();
                int tester02 = ran.nextInt(50000);
                String Checkn0 = Search.getString("NICKCHECKER0", null);
                String THESEARCHSTRING0 = Search.getString("NCSEARCHER0", null);
                String THELocation0 = Search.getString("NICKLOCATION0", null);
                String Tester0 = Search.getString(Checkn0, null) + "<<"
                        + tester02 + ">>";
                String Tester00 = Tester0.replace("AUTORUNNER-1",
                        "AUTORUNNER-0");
                SearchE.putString(Checkn0, Tester0);
                SearchE.commit();
                int IndexS0, IndexF0;
                int looper = 0;
                String local0 = THELocation0;
                String checknn0 = null;
                String AUTOSET0 = null;
                String AUTONAME0 = null;
                try {
                    mWakeLock0.acquire();
                    while (looper <= 10
                            && Tester0.contentEquals(Search.getString(Checkn0,
                                    null)) == true
                            || Tester00.contentEquals(Search.getString(Checkn0,
                                    null)) == true) {
                        System.gc();
                        while (Tester0.contentEquals(Search.getString(Checkn0,
                                null)) == true && looper <= 50) {
                            try {
                                Thread.sleep(2000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            NPOCHECKING.append(Search.getString(
                                    "AUTOMATIONNPOITEMSLIST", null));
                            // //getid
                            checknn0 = Search.getString(Checkn0, null);
                            IndexS0 = checknn0.indexOf("xjaszsetS[") + 10;
                            IndexF0 = checknn0.indexOf("]xjaszsetF");
                            AUTOSET0 = checknn0.substring(IndexS0, IndexF0);
                            IndexS0 = checknn0.indexOf("xjasznameS[") + 11;
                            IndexF0 = checknn0.indexOf("]xjasznameF");
                            AUTONAME0 = checknn0.substring(IndexS0, IndexF0);
                            RUNCHECKER CHECKER = new RUNCHECKER();
                            try {
                                String returned = CHECKER.GETCHECKDATA(
                                        THESEARCHSTRING0, NPOCHECKING);
                                if (returned.contains("--XJASZ1985--") == true) {
                                    SharedPreferences prime = getSharedPreferences(
                                            DEFAULTNAME, 0);
                                    SharedPreferences.Editor primeE = prime
                                            .edit();
                                    primeE.putString(Checkn0 + "Data", returned
                                            + "--XJASZ1--" + local0
                                            + "--XJASZ2--");
                                    primeE.commit();
                                    looper = 0;
                                    if (AUTOSET0.contentEquals("NICKX0") == true) {
                                        try {
                                            Class classRunning0 = Class
                                                    .forName("com.site.example.Running0");
                                            Intent intentRunning0 = new Intent(
                                                    AUTOAlarmService.this,
                                                    classRunning0);
                                            PendingIntent pi0 = PendingIntent
                                                    .getActivity(
                                                            getBaseContext(),
                                                            0, intentRunning0,
                                                            0);
                                            String body0 = "You have results for "
                                                    + AUTONAME0;
                                            String title0 = "Results Found";
                                            Notification n0 = new Notification(
                                                    R.drawable.ccnotification,
                                                    body0,
                                                    System.currentTimeMillis());
                                            n0.setLatestEventInfo(
                                                    AUTOAlarmService.this,
                                                    title0, body0, pi0);
                                            n0.flags = Notification.FLAG_AUTO_CANCEL;
                                            n0.sound = Uri
                                                    .parse("android.resource://"
                                                            + getPackageName()
                                                            + "/"
                                                            + R.raw.posting);
                                            n0.defaults = Notification.DEFAULT_LIGHTS
                                                    | Notification.DEFAULT_VIBRATE;
                                            numz = 0;
                                            Thread.currentThread().setName(
                                                    "NickT0");
                                            String meeee = Thread
                                                    .currentThread().getName()
                                                    .toString();
                                            SearchE.putString("NickT0", meeee);
                                            SearchE.commit();
                                            NotificationManager nm0 = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                                            nm0.notify(uniqueID[numz], n0);
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }
                            } catch (Exception e1) {
                                e1.printStackTrace();
                                looper++;
                                try {
                                    Thread.sleep(5000);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                            if (looper == 0) {
                                try {
                                    Thread.sleep(15000);// /////////////////////////////////
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            } else if (looper == 11) {
                                try {
                                    looper = 99;
                                    checknn0 = Search.getString(Checkn0, null);
                                    IndexS0 = checknn0.indexOf("xjasznameS[") + 11;
                                    IndexF0 = checknn0.indexOf("]xjasznameF");
                                    AUTONAME0 = checknn0.substring(IndexS0,
                                            IndexF0);
                                    Class classRunning10 = Class
                                            .forName("com.site.example.Home");
                                    Intent intentRunning10 = new Intent(
                                            AUTOAlarmService.this,
                                            classRunning10);
                                    PendingIntent pi10 = PendingIntent
                                            .getActivity(getBaseContext(), 0,
                                                    intentRunning10, 0);
                                    String body10 = AUTONAME0
                                            + " doesn't seem to be working?";
                                    String title10 = "The Results";
                                    Notification n10 = new Notification(
                                            R.drawable.ccnotification, body10,
                                            System.currentTimeMillis());
                                    n10.setLatestEventInfo(
                                            AUTOAlarmService.this, title10,
                                            body10, pi10);
                                    n10.flags = Notification.FLAG_AUTO_CANCEL;
                                    n10.sound = Uri.parse("android.resource://"
                                            + getPackageName() + "/"
                                            + R.raw.error);
                                    n10.defaults = Notification.DEFAULT_LIGHTS
                                            | Notification.DEFAULT_VIBRATE;
                                    numz = 10;
                                    NotificationManager nm10 = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                                    nm10.notify(uniqueID[numz], n10);
                                } catch (ClassNotFoundException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                        try {
                            Thread.sleep(10000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    mWakeLock0.release();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        mWakeLock0.release();
                    } catch (RuntimeException e) {
                        e.printStackTrace();
                    }
                    Thread.currentThread().setName("none0");
                    ender();
                }
            }
        });
        AUTOMATIONTIMER.start();
    }

-- Second to last this is the makeup of the one of the NICKX methods. These methods get put into a loop running automated searches until the user selects an option to shut off the thread. At that time it breaks out of the loop and kills the thread. If there are no other threads running when ender() method runs then the service gets killed. I know it might be hard to read but I spent a day coding it and each method runs perfect. The issue is it stacks up memory in the service overtime. I guess I'll add ender() for shits and giggles.

private void ender() {
    try {
        Thread.sleep(250);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    SharedPreferences Search = getSharedPreferences(DEFAULTNAME, 0);
    String me0 = Search.getString("NickT0", "XJASZZZZZZZZZZZ");
    String me1 = Search.getString("NickT1", "XJASZZZZZZZZZZZ");
    String me2 = Search.getString("NickT2", "XJASZZZZZZZZZZZ");
    String me3 = Search.getString("NickT3", "XJASZZZZZZZZZZZ");
    String me4 = Search.getString("NickT4", "XJASZZZZZZZZZZZ");
    String me5 = Search.getString("NickT5", "XJASZZZZZZZZZZZ");
    String me6 = Search.getString("NickT6", "XJASZZZZZZZZZZZ");
    String me7 = Search.getString("NickT7", "XJASZZZZZZZZZZZ");
    String me8 = Search.getString("NickT8", "XJASZZZZZZZZZZZ");
    String me9 = Search.getString("NickT9", "XJASZZZZZZZZZZZ");
    Map<Thread, StackTraceElement[]> mee = Thread.getAllStackTraces();
    String C = mee.toString();
    if (C.contains(me0) == true) {
    } else if (C.contains(me1) == true) {
    } else if (C.contains(me2) == true) {
    } else if (C.contains(me3) == true) {
    } else if (C.contains(me4) == true) {
    } else if (C.contains(me5) == true) {
    } else if (C.contains(me6) == true) {
    } else if (C.contains(me7) == true) {
    } else if (C.contains(me8) == true) {
    } else if (C.contains(me9) == true) {
    } else {
        stopService(new Intent(this, AUTOAlarmService.class));
    }
}

-- Ok ender() is called whenever a thread is ending. It checks to see if there are any other threads running. If there are none it kills the service which destroys the threads. It works fine. I figured I would add it just to show basically how this service runs from start to finish.

Can I get help with clearing my service memory? Is there some obscure method I can use? I've been combing through Google android methods and cannot find what I'm looking for.