我是Android应用程序开发人员的初学者,下面是我的程序,但是,当它运行时,在LogCat中,GC_CONCURRENT消息发生,我明白这是由于我的程序消耗了太多的memery,但是,通过阅读我的代码反复,我仍然不知道为什么我的程序会消耗比我预期的更多的内存,任何人都可以帮我看看并给我一些建议,谢谢!
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%免费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));
}
}
答案 0 :(得分:0)
确保你清除
NdefMessage msg = new NdefMessage(new NdefRecord[]{record});
msgs = new NdefMessage[]{msg};
主要的总消息有多少?为什么不做一个Log.debug(“LOG_TAG”,msg.length()) 并找出答案。
你还在模拟器上运行吗?如果是这样的内存设置是什么?
显然,你唯一要消耗内存的是上面的行,所以也许你需要在某些时候释放它。您也可以在eclipse中运行内存分析器并获取更多信息。