我有一个带有listview的应用程序。它的顶部有一个editext来过滤文本和单元格(搜索字段)。但是,根据应用程序崩溃的时间而定。我已经研究了该错误,似乎是内存泄漏问题。内存泄漏是由于GC无法检测到某些项目而引起的,并且由于它具有过滤功能,我想这可能确实是内存泄漏问题,对吧?
我得到的错误是这个
--------- beginning of crash
2018-10-31 15:34:49.896 2389-2389/? A/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
2018-10-31 15:34:49.896 2389-2389/? A/DEBUG: Build fingerprint: 'xiaomi/whyred/whyred:8.1.0/OPM1.171019.011/V10.0.2.0.OEIMIFH:user/release-keys'
2018-10-31 15:34:49.896 2389-2389/? A/DEBUG: Revision: '0'
2018-10-31 15:34:49.896 2389-2389/? A/DEBUG: ABI: 'arm64'
2018-10-31 15:34:49.897 2389-2389/? A/DEBUG: pid: 31358, tid: 2385, name: Filter >>> com.appdidier.atend <<<
2018-10-31 15:34:49.897 2389-2389/? A/DEBUG: signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
2018-10-31 15:34:49.916 2389-2389/? A/DEBUG: Abort message: 'Could not create epoll instance: Too many open files'
2018-10-31 15:34:49.916 2389-2389/? A/DEBUG: x0 0000000000000000 x1 0000000000000951 x2 0000000000000006 x3 0000000000000008
2018-10-31 15:34:49.916 2389-2389/? A/DEBUG: x4 fefeff7ddf6a4487 x5 fefeff7ddf6a4487 x6 fefeff7ddf6a4487 x7 7f7f7f7fff7f7fff
2018-10-31 15:34:49.916 2389-2389/? A/DEBUG: x8 0000000000000083 x9 3f8307f479ae5397 x10 0000000000000000 x11 0000000000000001
2018-10-31 15:34:49.916 2389-2389/? A/DEBUG: x12 ffffffffffffffff x13 ffffffffffffffff x14 ffffffffff000000 x15 ffffffffffffffff
2018-10-31 15:34:49.916 2389-2389/? A/DEBUG: x16 00000003eac72fa8 x17 0000007fa377664c x18 0000007f2040e870 x19 0000000000007a7e
2018-10-31 15:34:49.916 2389-2389/? A/DEBUG: x20 0000000000000951 x21 0000000000000001 x22 0000007ee06b4588 x23 0000007ee06b4588
2018-10-31 15:34:49.916 2389-2389/? A/DEBUG: x24 0000000000000004 x25 0000007ee06b4588 x26 0000007f02bea4a0 x27 0000000000000001
2018-10-31 15:34:49.916 2389-2389/? A/DEBUG: x28 0000007ee06b4588 x29 0000007ee06b3900 x30 0000007fa372beac
2018-10-31 15:34:49.916 2389-2389/? A/DEBUG: sp 0000007ee06b38c0 pc 0000007fa372bec8 pstate 0000000060000000
2018-10-31 15:34:50.132 29956-29979/? I/Finsky: [14364] com.google.android.finsky.bn.an.run(6): Stats for Executor: BlockingExecutor com.google.android.finsky.bn.ao@3b878ae[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 7]
2018-10-31 15:34:50.133 29956-29979/? I/Finsky: [14364] com.google.android.finsky.bn.an.run(6): Stats for Executor: LightweightExecutor com.google.android.finsky.bn.ao@ee4964f[Running, pool size = 4, active threads = 0, queued tasks = 0, completed tasks = 42]
2018-10-31 15:34:50.153 11286-11303/? W/LocalConnector: Cannot connect the wmserver-data-reciever
2018-10-31 15:34:50.206 29956-29979/? I/Finsky: [14364] com.google.android.finsky.bn.an.run(6): Stats for Executor: bgExecutor com.google.android.finsky.bn.ao@d3a41dc[Running, pool size = 4, active threads = 0, queued tasks = 0, completed tasks = 9]
2018-10-31 15:34:50.338 2389-2389/? A/DEBUG: backtrace:
2018-10-31 15:34:50.338 2389-2389/? A/DEBUG: #00 pc 000000000001dec8 /system/lib64/libc.so (abort+104)
2018-10-31 15:34:50.338 2389-2389/? A/DEBUG: #01 pc 0000000000007f20 /system/lib64/liblog.so (__android_log_assert+304)
2018-10-31 15:34:50.338 2389-2389/? A/DEBUG: #02 pc 000000000001570c /system/lib64/libutils.so (android::Looper::rebuildEpollLocked()+348)
2018-10-31 15:34:50.338 2389-2389/? A/DEBUG: #03 pc 000000000001556c /system/lib64/libutils.so (android::Looper::Looper(bool)+236)
2018-10-31 15:34:50.338 2389-2389/? A/DEBUG: #04 pc 000000000011993c /system/lib64/libandroid_runtime.so (android::NativeMessageQueue::NativeMessageQueue()+160)
2018-10-31 15:34:50.338 2389-2389/? A/DEBUG: #05 pc 000000000011a26c /system/lib64/libandroid_runtime.so (android::android_os_MessageQueue_nativeInit(_JNIEnv*, _jclass*)+28)
2018-10-31 15:34:50.338 2389-2389/? A/DEBUG: #06 pc 00000000009c2c70 /system/framework/arm64/boot-framework.oat (offset 0x9c0000) (android.os.Binder.clearCallingIdentity [DEDUPED]+144)
2018-10-31 15:34:50.338 2389-2389/? A/DEBUG: #07 pc 00000000000f3470 /dev/ashmem/dalvik-jit-code-cache (deleted)
我的代码中进行适配器设置的部分是这个:
myCustomAdapter = new CustomAdapter(listUsuarios);
listView.setAdapter(myCustomAdapter);
setupListeners();
myCustomAdapter.notifyDataSetChanged();
这是搜索edittext的关键侦听器
edtSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
previousLength = edtSearch.getText().length();
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (previousLength > edtSearch.getText().length()) {
myCustomAdapter.setDataObjects(listUsuarios);
}
myCustomAdapter.getFilter().filter(edtSearch.getText().toString());
myCustomAdapter.notifyDataSetChanged();
}
@Override
public void afterTextChanged(Editable editable) {
}
});
这是我的自定义适配器类的一部分,我认为对这个问题很重要(我怀疑内存泄漏问题可能在getFilter方法上):
public class CustomAdapter extends BaseAdapter {
private List<Usuario> dataObjects;
public List<Usuario> getDataObjects() {
return dataObjects;
}
public void setDataObjects(List<Usuario> dataObjects) {
this.dataObjects = dataObjects;
}
public CustomAdapter(List<Usuario> dataObjects) {
this.dataObjects = dataObjects;
}
@Override
public int getCount() {
return dataObjects.size();
}
@Override
public Object getItem(int i) {
return dataObjects.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults oReturn = new FilterResults();
final ArrayList<Usuario> results = new ArrayList<Usuario>();
if (constraint != null) {
if (dataObjects != null && dataObjects.size() > 0) {
for (final Usuario g : dataObjects) {
if (g.getNome().toLowerCase()
.contains(constraint.toString()))
results.add(g);
}
}
oReturn.values = results;
}
return oReturn;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
// Log.d("MYTAG", "" + dataObjects.get(0).getNome());
dataObjects = (ArrayList<Usuario>) results.values;
notifyDataSetChanged();
}
};
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}