StuStirling
StuStirling

Reputation: 16221

Updating TextView's Text Causing GC_CONCURRENT Issue

My Problem

I have a timer which is initalized like so and calls a method to update an object. The problem is I am getting GC_CONCURRENT outputs in my LogCat every time the timer is fired.

11-01 11:56:43.042: D/dalvikvm(30950): GC_CONCURRENT freed 774K, 52% free 3303K/6791K, external 1763K/2129K, paused 3ms+4ms

Here is my timer initialisation along with the task.

    if (CCTrackManager.timer == null) 
        CCTrackManager.timer = new Timer("TrackUpdate");
    MyTimerTask task = new MyTimerTask();
    CCTrackManager.timer.scheduleAtFixedRate(task, 0, 1000);

So this is called once and not called again, (unless the timer is stopped and restarted).

My update method looks as so.

 public void addSecondToTrackDuration() {
      this._currentRecordingTrack.setDuration(this._currentRecordingTrack.getDuration() + 1);
      this.mListener.trackUpdated(this._currentRecordingTrack);
  }

Then here is my Handler which I use to handle getting the value from this thread into my UI thread.

//----------------------------------------------------------------------
    // HANDLES THE UPDATE OF THE TRACK. NEEDS HANDLER BECAUSE THE TIMER WHICH CALLS THIS RUNS ON ANOTHER THREAD 
    //----------------------------------------------------------------------
    static class UpdateTrackHandler extends Handler {

        WeakReference<MainActivity> mActivity;
        UpdateTrackHandler(MainActivity act) {
            mActivity = new WeakReference<MainActivity>(act);
        }

        @Override 
        public void handleMessage(Message msg) {
            TrackFragment tf = (TrackFragment) mActivity.get().getSupportFragmentManager().findFragmentByTag(RECORDING_TRACK_FRAG);
            if (tf != null) {
                tf.updateViewWithTrack((CCTrack)msg.obj);
            }
        }
    }

    UpdateTrackHandler mHandler = new UpdateTrackHandler(this);

    //----------------------------------------------------------------------
        // CALLED FROM CCTRACKMANAGER WHEN AN UPDATE OCCURS ON THE  CURRENT RECORDING TRACK
        //---------------------------------------------------------------------- 
        public void trackUpdated(CCTrack track) {
            Message msg = new Message();
            msg.obj = track;
            mHandler.sendMessage(msg);
        }

I don't think this is the problem. I think the problem is in the fragment where I am actually updating the TextView as when I comment it out this.durationValueTxtView.setText("TIME");, it doesn't happen anymore.

public void updateViewWithTrack(CCTrack track) {
    this.durationValueTxtView.setText("TIME");
}

My Question

Am I missing something in terms of my handler creation, timer creation, timer task creation, textview reference? Anything?

Thanks in advance

Upvotes: 1

Views: 544

Answers (2)

Gaurav Arora
Gaurav Arora

Reputation: 17274

Why are you sending CCTrack instance in updateViewWithTrack(CCTrack track) when you aren't using it ? Try removing it, your code might work fine.

  • GC_CONCURRENT is thrown whenever is a memory leak in your code.
  • Try reading this and this

Upvotes: 1

piotrpo
piotrpo

Reputation: 12636

It seams that you simply try to update UI from outside of UI thread - i.e. from TimerTask You must only post message to handler and update UI from handler code. Take a look here: Android timer? How-to?

Upvotes: 0

Related Questions