0

I am a beginner of android application developer, below is my program, however, when it runs, in the LogCat, GC_CONCURRENT messages occur, I understand it is caused by my program consuming too much memery, however, by reading my code repeatedly, I still have no idea why my program will consume much more memory than I expected, can anyone help me to take a look and give some advice to me, thank you!

07-11 10:38:12.258: D/dalvikvm(10060): GC_CONCURRENT freed 88K, 2% free 12912K/13063K, paused 2ms+13ms
07-11 10:38:25.024: D/dalvikvm(10060): GC_CONCURRENT freed 84K, 2% free 13249K/13447K, paused 1ms+3ms
Below is the code:

public class Reader extends Activity {

TextView mText;
NfcAdapter mAdapter;
PendingIntent mPendingIntent;
IntentFilter mFilters[];
String mTechLists[][];

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public void onStart(){
    super.onStart();
    setContentView(R.layout.activity_reader);

    mText = (TextView) findViewById(R.id.text);

    mAdapter = NfcAdapter.getDefaultAdapter(this);
    mPendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    try{
        ndef.addDataType("text/plain");
    }catch(MalformedMimeTypeException e){
        throw new RuntimeException("fail", e);
    }

    IntentFilter nt = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    mFilters = new IntentFilter[]{
            ndef, nt
    };

    mTechLists = new String[][]{
            new String[]{
                    Ndef.class.getName()
            }
    };
    Intent intent = getIntent();
    mText.setText(getNdefMessages(intent));
}

public String getNdefMessages(Intent intent){
    NdefMessage[] msgs = null;
    String action = intent.getAction();
    if(NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)||
            NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)){
        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        if(rawMsgs != null){
            msgs = new NdefMessage[rawMsgs.length];
            for(int i=0; i<rawMsgs.length; i++){
                msgs[i] = (NdefMessage) rawMsgs[i];
            }
        }else{
            byte[] empty = new byte[]{};
            NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, empty, empty);
            NdefMessage msg = new NdefMessage(new NdefRecord[]{record});
            msgs = new NdefMessage[]{msg};
        }

    }
    else {
        finish();
    }
    if(msgs==null)
        return "No Tag discovered!";
    else
        return msgs.toString();
}

@Override
public void onResume(){
    super.onResume();
    if (mAdapter != null)
        mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists);

}

@Override
public void onPause(){
    super.onPause();
    if (mAdapter != null)
        mAdapter.disableForegroundDispatch(this);
}

@Override
public void onNewIntent(Intent intent){
    Log.i("Foreground dispatch", "Discovered tag with intent:" + intent);
    mText = (TextView) findViewById(R.id.text);
    mText.setText(getNdefMessages(intent));
}

}
Conrad
  • 933
  • 4
  • 19
  • 34
  • Check this link, i think your heap is full http://stackoverflow.com/questions/6591807/how-to-solve-gc-concurrent-freed – Vishwanath.M Jul 11 '12 at 04:16
  • yam I understand why there are such kind of message, so I would like to ask is how can I correct my program to fix it, but currently, I still cannot write the data into the tag, so I do not know whether the heap size is big enough to suit my need right now, anyway, thank you man – Conrad Jul 11 '12 at 06:24

1 Answers1

0

Well make sure that you clear out

  NdefMessage msg = new NdefMessage(new NdefRecord[]{record});
        msgs = new NdefMessage[]{msg};

How main total msgs are there? Why not do a Log.debug("LOG_TAG", msg.length()) and find out.

Also are you running on emulator? if so what are the memory settings?

Clearly the only thing you are doing to consume memory is the lines above, so perhaps you need to free this up at some point. You can also run the memory analyzer in eclipse and get more information too.

Code Droid
  • 10,344
  • 17
  • 72
  • 112
  • Actually, I am using the Galaxy Nexus to do the testing, and currently I just try to open the app without using a tag, it works after I hide the finish() function, I am not sure where it works. And I am going to try with a tag which has mutiple records inside. But currently I the app I am using to write the tag only support writing a single message inside, which does not fulfill my requirement of writing several records and an AAR record inside, would anyone gives a suggestion of android app which can do so, or is there other method to write NFC tag record? – Conrad Jul 11 '12 at 04:29
  • I would post this as a separate question. – Code Droid Jul 11 '12 at 21:38
  • Also why answer the questions I have asked you in trying to diagnose? – Code Droid Jul 11 '12 at 21:39
  • The other thing you can do is just comment out swaths of code an see if you still get the error message. – Code Droid Jul 11 '12 at 21:39
  • oh, sorry, I forgot to reply, ya, just like you have mentioned, I think I have solved the problem by commenting out some codes. – Conrad Jul 12 '12 at 02:10