'Memory Leak on Periodic Service Task called with BroadcastReceiver

I have a periodic service task set with an Alarm and a broadcastreceiver. After it finishes its work, a broadcast is fired ON-destroy and the task is killed to be restarted.

I pass a variable from this service task, and send it back to its origin activity with sendbroadcast method. When I check the ram usage of the app, I see an increase overtime. Why does this happen? Is this related with my misuse of broadcastreceiver?

Here is the main activity:

public class MainActivity : Activity
{
    int count = 1;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        
        SetAlarmForBackgroundServices(this);
    }

    public static void SetAlarmForBackgroundServices(Context context)
    {
        var alarmIntent = new Intent(context.ApplicationContext, typeof(AlarmReceiver));
        var broadcast = PendingIntent.GetBroadcast(context.ApplicationContext, 0, alarmIntent, PendingIntentFlags.NoCreate);
        if (broadcast == null)
        {
            var pendingIntent = PendingIntent.GetBroadcast(context.ApplicationContext, 0, alarmIntent, 0);
            var alarmManager = (AlarmManager)context.GetSystemService(Context.AlarmService);
            alarmManager.Set(AlarmType.ElapsedRealtimeWakeup,2000, pendingIntent);
        }
    }
}

And here is the broadcastreceiver:

[BroadcastReceiver]
class AlarmReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        var backgroundServiceIntent = new Intent(context, typeof(PeriodicBackgroundService));
        string RecorderVariable = intent.GetStringExtra("RecorderVariable");
        context.StartService(backgroundServiceIntent);
    }
}

}

My periodic service task is like this :

[Service]
 class PeriodicBackgroundService : Service
{
    private const string Tag = "[PeriodicBackgroundService]";

    private bool _isRunning;
    private Context _context;
    private Task _task;

    #region overrides

    public override IBinder OnBind(Intent intent)
    {
        return null;
    }

    public override void OnCreate()
    {
        _context = this;
        _isRunning = false;
        _task = new Task(DoWork);
    }

    public override void OnDestroy()
    {


          var alarmIntent2 = new Intent(this.ApplicationContext, typeof(AlarmReceiver));
          alarmIntent2.PutExtra("RecorderVariable", RecorderVariable);
          SendBroadcast(alarmIntent2);
       

       /////
        _isRunning = false;

        if (_task != null && _task.Status == TaskStatus.RanToCompletion)
        {
            _task.Dispose();
        }
    }

    public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
    {  

       

        if (!_isRunning)
        {
            _isRunning = true;
            _task.Start();
        }
        return StartCommandResult.Sticky;
    }

    #endregion

    private void DoWork()
    {
        try
        {
            /////some work is done here........

        }
        catch (Exception e)
        {
            Log.WriteLine(LogPriority.Error, Tag, e.ToString());
        }
        finally
        {
            
            StopSelf();
        }
    }
}

}



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source