У меня IntentService
, в котором я повторяю a Runnable
каждую секунду .
В пределах Runnable
Язапрашивает записи у MyItemsRepository
, который использует ObjectBox .
Где-то утечка памяти . Android Profiler показывает огромное потребление "Native" .Метод getAllByDate()
выделяется много.
Кажется, это утечка, но я не знаю, как это исправить.
Есть идеи?Заранее большое спасибо!
Приложение
public class App extends Application {
@Nullable
private BoxStore boxStore;
private static App instance;
public static App getApp() {
return instance;
}
public BoxStore getBoxStore() {
if (boxStore == null) {
boxStore = MyObjectBox.builder().androidContext(App.this).build();
}
return boxStore;
}
@Override
public void onCreate() {
super.onCreate();
Intent service = new Intent(this, MyService.class);
this.startService(service);
}
}
MyService
public class MyService extends IntentService {
private final static String TAG = "MyService";
@Nullable
private Runnable runnable;
@Nullable
private List<Item> myItems;
public MyService() {
super(TAG);
myItems = new ArrayList<>();
}
@Override
public void onDestroy () {
super.onDestroy();
myItems = null; // trying to get rid off the allocation?
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
HandlerThread handlerThread = new HandlerThread(TAG + "_handlerThread");
handlerThread.start();
Handler handler = new Handler(handlerThread.getLooper());
runnable = () -> {
try {
processMyItems();
if (runnable != null) {
handler.postDelayed(runnable, 1000); // restart
}
} catch (InterruptedException e) {
e.printStackTrace();
if (runnable != null) {
handler.postDelayed(runnable, 1000); // retry
}
} catch (Exception e) {
e.printStackTrace();
}
};
handler.postDelayed(runnable, 1000); // first run
}
protected void processMyItems() throws InterruptedException, Exception {
myItems = MyItemsRepository.getInstance().getAllByDate();
// ... do stuff with myItems ...
}
}
MyItemsRepository
public class MyItemsRepository {
private final static String TAG = "MyItemsRepository";
private static MyItemsRepository instance;
@Nullable
private Box<MyItem> box;
/**
* Singleton
* @return old or new instance
*/
public static MyItemsRepository getInstance() {
if (MyItemsRepository.instance == null) {
Logger.d(TAG, "new instance");
MyItemsRepository.instance = new MyItemsRepository();
}
return MyItemsRepository.instance;
}
public List<MyItems> getAllByDate() {
if (box == null) {
return new ArrayList<>();
}
ElapsedTimer et = new ElapsedTimer();
List<MyItem> items = box.query()
.order(MyItem_.createdAt, QueryBuilder.DESCENDING)
.build()
.find();
Logger.d(TAG, "getAllByDate() " + et.getElapsedMillisAsString());
return items;
}
}
ElapsedTimer (Помощник)
public class ElapsedTimer {
private long startTime;
public ElapsedTimer() {
startTime = System.currentTimeMillis();
}
public String getElapsedMillisAsString() {
return String.format("Elapsed: %s ms", getElapsedMillis());
}
}