0

我是android应用程序开发人员的初学者,下面是我的程序,但是,当它运行时,在LogCat中,出现GC_CONCURRENT消息,我理解这是我的程序消耗太多内存造成的,但是,通过反复阅读我的代码,我仍然不知道为什么我的程序会消耗比我预期的更多的内存,任何人都可以帮我看看并给我一些建议,谢谢!

07-11 10:38:12.258: D/dalvikvm(10060): GC_CONCURRENT 释放 88K, 2% 释放 12912K/13063K, 暂停 2ms+13ms
07-11 10:38:25.024: D/dalvikvm(10060): GC_CONCURRENT 释放 84K , 2% free 13249K/13447K, 暂停1ms+3ms
下面是代码:

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

}
4

1 回答 1

0

那么确保你清除

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

有多少主要的总味精?为什么不做一个 Log.debug("LOG_TAG", msg.length()) 并找出答案。

你也在模拟器上运行吗?如果是这样,内存设置是什么?

显然,您消耗内存的唯一事情就是上面的行,所以也许您需要在某个时候释放它。您还可以在 eclipse 中运行内存分析器并获取更多信息。

于 2012-07-11T04:04:51.723 回答