简体   繁体   中英

GC_CONCURRENT solving

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));
}

}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM