私はリサイクルビューを上下に繰り返し移動するにつれ、割り当てられたメモリが増えていることに気付きました。何回も移動するとメモリ消費量が最大100MBになることがあります。recyclerview onBindメソッドで "new"を使用していますか?
onBindViewHolderに私は、このメソッドはonBindViewHolderから呼び出されているので、私は上下にスクロール毎回だと思うものを、コードから判断
public void displayDate(MessageViewHolder holder, int position){
DateTime currentDate = new DateTime(getMessage(position).getSentDate());
DateTimeFormatter builder = DateTimeFormat.forPattern("hh:mm a");
String currentDateString = builder.print(currentDate);
holder.date.setText(currentDateString);
holder.date.setVisibility(View.VISIBLE);
/*
Display date only if the next message is not mine, or its date
is different to the current message by 1 min
*/
if(position+1<messages.size() && (getMessage(position+1).isMine() == getMessage(position).isMine())){
DateTime nextDate = new DateTime(getMessage(position+1).getSentDate());
if(builder.print(nextDate).equals(currentDateString)) {
holder.date.setVisibility(View.GONE);
}
}
}
この関数を呼び出すので、私は、アイテムの日付を表示する必要があります私は "new"を使用して以来、常に日付オブジェクトが作成されるため、再度上下にスクロールするたびにメモリ割り当てが増えます。
onBindViewHolderで「new」を呼び出すのは悪い習慣ですか? また、私は最適化したものを超えていますし、その仕事をするためにGCを残すべきですか?