1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-29 13:26:50 +01:00

Bangle.js: fix memory leak from HTTP requests

Every call to Volley.newRequestQueue() creates a new global thread pool,
which isn't automatically cleaned up once the request completes.
With this commit we create a RequestQueue around on first use, and reuse
it for subsequent requests.
This commit is contained in:
Richard de Boer 2023-05-31 20:48:06 +02:00 committed by José Rebelo
parent 0c52f3d3da
commit 7e1685f5f9

View File

@ -160,6 +160,9 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
// this stores the globalUartReceiver (for uart.tx intents) // this stores the globalUartReceiver (for uart.tx intents)
private BroadcastReceiver globalUartReceiver = null; private BroadcastReceiver globalUartReceiver = null;
// used to make HTTP requests and handle responses
private RequestQueue requestQueue = null;
/// Maximum amount of characters to store in receiveHistory /// Maximum amount of characters to store in receiveHistory
public static final int MAX_RECEIVE_HISTORY_CHARS = 100000; public static final int MAX_RECEIVE_HISTORY_CHARS = 100000;
/// Used to avoid spamming logs with ACTION_DEVICE_CHANGED messages /// Used to avoid spamming logs with ACTION_DEVICE_CHANGED messages
@ -184,6 +187,7 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
super.dispose(); super.dispose();
stopGlobalUartReceiver(); stopGlobalUartReceiver();
stopLocationUpdate(); stopLocationUpdate();
stopRequestQueue();
} }
private void stopGlobalUartReceiver(){ private void stopGlobalUartReceiver(){
@ -199,6 +203,19 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
gpsUpdateSetup = false; gpsUpdateSetup = false;
} }
private void stopRequestQueue() {
if (requestQueue != null) {
requestQueue.stop();
}
}
private RequestQueue getRequestQueue() {
if (requestQueue == null) {
requestQueue = Volley.newRequestQueue(getContext());
}
return requestQueue;
}
private void addReceiveHistory(String s) { private void addReceiveHistory(String s) {
receiveHistory += s; receiveHistory += s;
if (receiveHistory.length() > MAX_RECEIVE_HISTORY_CHARS) if (receiveHistory.length() > MAX_RECEIVE_HISTORY_CHARS)
@ -598,7 +615,6 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
final String id = _id; final String id = _id;
if (BuildConfig.INTERNET_ACCESS && devicePrefs.getBoolean(PREF_DEVICE_INTERNET_ACCESS, false)) { if (BuildConfig.INTERNET_ACCESS && devicePrefs.getBoolean(PREF_DEVICE_INTERNET_ACCESS, false)) {
RequestQueue queue = Volley.newRequestQueue(getContext());
String url = json.getString("url"); String url = json.getString("url");
int method = Request.Method.GET; int method = Request.Method.GET;
@ -691,6 +707,7 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
return h; return h;
} }
}; };
RequestQueue queue = getRequestQueue();
queue.add(stringRequest); queue.add(stringRequest);
} else { } else {
if (BuildConfig.INTERNET_ACCESS) if (BuildConfig.INTERNET_ACCESS)