I have a memory leak in my android application. It is a simple music player. At the bottom of the screen, I have a TextView which I use to display the elapsed time. It is updated in the thread below.
Each time I change the orientation, the heap size grows. From looking at the DDMS heap updates, it looks like my activity isn't being garbage collected. If, however, I comment out the 6 lines as I have done below, the GC keeps the heap at a fairly consistent size. Could you please let me know what it is that's causing this leak?
private void updateTimerAndSeekBar() {
Thread updater = new Thread() {
SeekBar seekbar = (SeekBar) findViewById(R.id.seekBar1);
TextView timer = (TextView) findViewById(R.id.currentTime);
public void run() {
while (mediaPlayer.isPlaying()) {
// runOnUiThread(new Runnable() {
// #Override
// public void run() {
// timer.setText(msToMins(mediaPlayer.getCurrentPosition()));
// }
// });
try {
seekbar.setProgress(mediaPlayer.getCurrentPosition());
sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
updater.start();
}
Each time you change the orientation, Android creates and starts a new activity. The memory leak is due to the fact that the garbage collector cannot collect the old activity that's no longer needed since the separate thread is still running and - as an inner class - is holding onto the activity.
To get rid of the memory leak, you need to stop and end the separate thread when the activity is destroyed.
boolean stopThread;
private void updateTimerAndSeekBar() {
stopThread = false;
Thread updater = new Thread() {
...
while (!stopThread && mediaPlayer.isPlaying()) {
try {
seekbar.setProgress(mediaPlayer.getCurrentPosition());
sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
updater.start();
}
protected void onDestroy () {
stopThread = true;
super.onDestroy();
}
Related
To prevent ANR on Android you could implement a Watchdog Timer which would require to have another Thread running and waiting for heartbeats. If you expect your App to have a refresh rate of 30FPS, guard for a refresh rate of lower than 15FPS. This covers also the case when you would have potentially an ANR in the future.
If you know the places where the UI might get stuck for a long time, you can build the watchdog to guard against this (skip current frame for example). If it is to long why are you doing it on the UI? If the action you are performing refers to redrawing the screen you can not do it somewhere else.
Here I made a simple example of a watchdog.
public class MainActivity extends AppCompatActivity {
int count = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread watchdogThread = new Thread(new Runnable() {
long lastT = System.currentTimeMillis();
long lastCount = 0;
long missed = 0;
#Override
public void run() {
while(true){
if (System.currentTimeMillis()-lastT>1500){
if (count == lastCount){
missed++;
}
lastCount = count;
if (missed > 3){
Log.e("test", "Error");
}
lastT = System.currentTimeMillis();
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
watchdogThread.start();
startCycleUI();
}
private void startCycleUI() {
//Runs on UI Thread
long lastT = System.currentTimeMillis();
while(true){
if (System.currentTimeMillis()-lastT>1000){
if (count < 10) {
count++;
}
lastT = System.currentTimeMillis();
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
When you do not know where to look, is there a way you could build a watchdog timer that simply restarts the application? How can this watchdog timer live beyond the application context?
You can use the example from "Java Source Code Warehouse" project
You have options to:
Monitor the memory in a particular process
Kill the process if they don't return
Check memory usage in the system, scheduling kills/reboots as needed.
public class CallEvent extends BroadcastReceiver{
public LEDController ledController = new LEDController();
public ApplicationSettings applicationSettings = new ApplicationSettings();
public boolean ring = false;
#Override
public void onReceive(Context context, Intent intent){
if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)){
ring = true;
blink();
}else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE) ||
intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
ring = false;
}
}
public void blink(){
Runnable r = new Runnable() {
#Override
public void run() {
while(ring){
ledController.turnOnFlash();
try {
Thread.sleep(applicationSettings.getDelayOn());
} catch (InterruptedException e) {
e.printStackTrace();
}
ledController.turnOffFlash();
try {
Thread.sleep(applicationSettings.getDelayOff());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread blinkThread = new Thread(r);
blinkThread.start();
}
}
I want to create led messenger when phone is ring. But I can't stop blinkThread. I dont know it is not working. Led start when call income but don't stop when call decline. Variable ring is changing on 'false' when call decline, but thread still working
In general, you don't forcibly stop threads because it's dangerous. You set a flag that tells the thread in question to exit from it's thread loop under controlled circumstances.
Your thread loop looks something along these lines:
void run() {
while (shouldContinue) {
doThreadWorkUnit();
}
}
And somewhere else you set the shouldContinue variable and wait for the thread to finish:
...
thread.shouldContinue = false;
thread.join();
...
(All this is likely not correct Java, since I don't do Java. View it as pseudo code and modify for your actual language/thread library/etc.)
Source: How to stop a thread?
How create a button which pause the thread which is inside the loop and another button which
resumes.
Runnable myRun = new Runnable(){
public void run(){
for(int j =0 ;j<=words.length;j++){
synchronized(this){
try {
wait(sleepTime);
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}});
bt2.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
notify();
}
});
} catch (InterruptedException e) {
e.printStackTrace();
} }
runOnUiThread(new Runnable(){
public void run(){
try {
et.setText(words[i]);
i++;
} catch (Exception e) {
e.printStackTrace();
}
}});
}}};
doing some stuff say words.lenght=1000 times
then suppose user want to take break in between
click pause button with id = bt this button pauses thread until and user
clicks resume with id= bt1
Below is a hint , i think you can use for your problem. Its copied from the link i pasted at end.
A wait can be "woken up" by another process calling notify on the monitor which is being waited on whereas a sleep cannot. Also a wait (and notify) must happen in a block synchronized on the monitor object whereas sleep does not:
Object mon = ...;
synchronized (mon) {
mon.wait();
}
At this point the currently executing thread waits and releases the monitor. Another thread may do
synchronized (mon) { mon.notify(); }(On the same mon object) and the first thread (assuming it is the only thread waiting on the monitor) will wake up.
Check Difference between wait() and sleep()
You do it like this:
How to indefinitely pause a thread in Java and later resume it?
Only you call the suspend() and other methods from your buttons' OnClickListeners
I have implemeted a custom camera and I want it to go, after a period of time into a standby state. The standby concsists from a stopPreview, a camera release and a view that tells the user to tap in order to exit the standby. Because I set the text within the new thread, I get the CalledFromWrongThreadException, but I don't know what the solution could be. I found other posts around, but none of them really worked.
Code:
private void initCamera()
{//more code
threadModifiedText = (TextView) findViewById(R.id.textView1);
Thread standbyThread = new Thread()
{
#Override
public void run()
{
try
{
while (timeCounter > 0)
{
if (!activeThread)
{
sleep(100);
if (timeCounter % 10 == 0)
{
threadHandler.sendEmptyMessage((int) timeCounter / 10);
}
timeCounter--;
}
}
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
mCamera.stopPreview();
mCamera.release();
TextView standbytext = new TextView(SlicesActivity.this);
standbytext.setText("Tap to exit standby mode");
FrameLayout preview = (FrameLayout) findViewById(id.FrameLayout_camera_preview);
preview.addView(standbytext);
}
};
standbyThread.start();
//more code}
And
#Override
public void onUserInteraction()
{
Log.d("~~~~~~~~~", "apasat");
activeThread = true;
timerCounter = 300;
}
private Handler threadHandler = new Handler()
{
public void handleMessage(android.os.Message msg)
{
// whenever the Thread notifies this handler we have
// only this behavior
threadModifiedText.setText("\ncounter is " + Integer.toString(msg.what));
}
};
Please guys, give me some suggestions. 10x
try to use
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO: update your UI here
}
});
in your thread to update UI
You cannot set text values inside any thread except the UI thread. Complete your background thread operations and set the text value after your operations are over in the UI thread.
Hi,
In my app im trying to use Thread.sleep(100) to pause my thread, while its backgrounded in order to use less cpu, but it freezes when I open it back up.
I realized that onResume is not being called when I reopen the app.
Any ideas why?
public void onPause() {
pause = true;
Log.d("mSTATE","THREADPAUSE");
}
public void onResume() {
pause = false;
running = true;
Log.d("mSTATE","THREADRESUME");
}
public void run() {
while(running){
while(pause && running){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
while (!pause && running) {
Canvas c = null;
try {
c = sHolder.lockCanvas(null);
synchronized (sHolder) {
doDraw(c);
powerUps();
}
} finally {
if (c != null) {
sHolder.unlockCanvasAndPost(c);
}
}
}
}
Log.d("mState","EndofRun");
}
When you put your thread to sleep you are also blocking the UI thread thus leading to freezing of your application.
You need to check the Threads in Android way for the best performance.
http://developer.android.com/resources/articles/painless-threading.html