Android RAM usage error with broadcast receiver in service

206 views Asked by At

I have written simple service which have one broadcast receiver that listens for incoming sms messages and write info about message to database. Problem is that every time message is received, RAM usage of service increases by 1-2 MB.

// Init
smsRecFilter.setPriority(999);
smsRecFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(smsReceiver, smsRecFilter);

// Implementation
private class SMSreceiver extends BroadcastReceiver {
   @Override
   public void onReceive(Context context, Intent intent) {
      Bundle extras = intent.getExtras();
      if (extras != null) {
      SmsMessage smsMsg;
      Object[] smsPdus = (Object[])extras.get("pdus");
      if (smsPdus != null) {
         InfoHolder holder = new InfoHolder();
         if (holder != null) {
            holder.Type = InfoTypes.SMS_REC;
            for (int i = 0; i < smsPdus.length; i++) {
               smsMsg = SmsMessage.createFromPdu((byte[])smsPdus[i]);
               if (smsMsg != null) {
                  if (i == 0) {
                     holder.Number = smsMsg.getOriginatingAddress();
                     holder.DateTime =  MillisToDate(smsMsg.getTimestampMillis());
                  }
                    holder.MsgBody += smsMsg.getMessageBody().toString();
                }
              }
                m_db.Insert(holder);
            }
        }
    }
}

}

UPDATE

The insert method:

my Insert() function:

private boolean Insert(InfoHolder holder)
    {
        try {
            ContentValues values = new ContentValues();
            if (values != null) {
                values.put( DBase.COLUMN_TYPE,      holder.Type);
                values.put( DBase.COLUMN_NUMBER,    holder.Number);
                values.put( DBase.COLUMN_MSGBODY,   holder.MsgBody);
                values.put( DBase.COLUMN_CALL_DUR,  holder.CallDuration);
                values.put( DBase.COLUMN_DATETIME,  holder.DateTime);
                mDB.insert(DB_TABLE_NAME, null, values);
                values.clear();
            }
        }
        catch( Exception e ) {return false;}

        return true;
    }
1

There are 1 answers

1
wezzy On

I can't see any evident memory leak, maybe you have a problem in the ORM layer when you call m_db.Insert(). Try calling System.gc() before the end to force the garbage collector to run and see if the RAM usage decrease