Skip to content

Commit

Permalink
Merge pull request #750 from AltBeacon/fix-android-8-out-of-memory-crash
Browse files Browse the repository at this point in the history
Prevent OutOfMemoryError Crash on Android 8
  • Loading branch information
davidgyoung authored Oct 17, 2018
2 parents 1990ba7 + e2aa6e0 commit 7e16c69
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### Development

- Prevent infrequent out of memory crashes on Android 8+ (#750 Pappas Christodoulos, David G. Young)
- Prevent duplicate ranging/monitoring callbacks casued by bind/unbind with a service
(#748, Adrián Nieto Rodríguez, #745, David G. Young)
- Allow starting foreground service at boot (#746, David G. Young)
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/altbeacon/beacon/service/ScanHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ void processScanResult(BluetoothDevice device, int rssi, byte[] scanRecord) {
new ScanHelper.ScanProcessor(nonBeaconLeScanCallback).executeOnExecutor(mExecutor,
new ScanHelper.ScanData(device, rssi, scanRecord));
} catch (RejectedExecutionException e) {

LogManager.w(TAG, "Ignoring scan result because we cannot keep up.");
} catch (OutOfMemoryError e) {
LogManager.w(TAG, "Ignoring scan result because we cannot start a thread to keep up.");
}
}

Expand Down
17 changes: 14 additions & 3 deletions src/main/java/org/altbeacon/beacon/service/ScanJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ public class ScanJob extends JobService {

@Override
public boolean onStartJob(final JobParameters jobParameters) {
initialzeScanHelper();
if (!initialzeScanHelper()) {
LogManager.e(TAG, "Cannot allocate a scanner to look for beacons. System resources are low.");
return false;
}
if (jobParameters.getJobId() == getImmediateScanJobId(this)) {
LogManager.i(TAG, "Running immediate scan job: instance is "+this);
}
Expand Down Expand Up @@ -168,7 +171,8 @@ private void stopScanning() {
LogManager.d(TAG, "Scanning stopped");
}

private void initialzeScanHelper() {
// Returns false if cycle thread cannot be allocated
private boolean initialzeScanHelper() {
mScanHelper = new ScanHelper(this);
mScanState = ScanState.restore(ScanJob.this);
mScanState.setLastScanStartTimeMillis(System.currentTimeMillis());
Expand All @@ -177,8 +181,15 @@ private void initialzeScanHelper() {
mScanHelper.setBeaconParsers(mScanState.getBeaconParsers());
mScanHelper.setExtraDataBeaconTracker(mScanState.getExtraBeaconDataTracker());
if (mScanHelper.getCycledScanner() == null) {
mScanHelper.createCycledLeScanner(mScanState.getBackgroundMode(), null);
try {
mScanHelper.createCycledLeScanner(mScanState.getBackgroundMode(), null);
}
catch (OutOfMemoryError e) {
LogManager.w(TAG, "Failed to create CycledLeScanner thread.");
return false;
}
}
return true;
}

// Returns true of scanning actually was started, false if it did not need to be
Expand Down

0 comments on commit 7e16c69

Please sign in to comment.