Skip to content

Commit

Permalink
Merge branch 'reduce_memory_usage_for_tile_layers'
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpoole committed Aug 27, 2023
2 parents 8c17c33 + 28c16aa commit 4c07e6d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/main/java/de/blau/android/DebugInformation.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.content.pm.ApplicationInfo;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Debug;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
Expand Down Expand Up @@ -94,7 +95,8 @@ String getDebugText(String eol) {
builder.append("Target SDK: " + appInfo.targetSdkVersion + eol);

builder.append("Maximum avaliable memory " + Runtime.getRuntime().maxMemory() + eol);
builder.append("Total memory used " + Runtime.getRuntime().totalMemory() + eol);
builder.append("Total memory used (non-native) " + Runtime.getRuntime().totalMemory() + eol);
builder.append("Native memory usage " + Debug.getNativeHeapAllocatedSize() + eol);
Logic logic = App.getLogic();
if (logic != null) {
Map map = logic.getMap();
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/de/blau/android/Map.java
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,14 @@ public void setUpLayers(@NonNull Context ctx) {
mLayers.clear();
mLayers.addAll(tempLayers);
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
// check memory usage and zap caches if necessary
// Android 8 and later allocate Bitmap storage natively
Runtime runtime = Runtime.getRuntime();
if (runtime.totalMemory() > runtime.maxMemory() / 2) {
flushInvisibleImageryCaches();
}
}
}

/**
Expand Down Expand Up @@ -438,6 +446,27 @@ public boolean isVisible(@NonNull MapViewLayer layer) {
return false;
}

/**
* Flush the in memory caches for all imagery layers except the top one if it is visible
*/
public void flushInvisibleImageryCaches() {
List<MapViewLayer> layers = new ArrayList<>();
synchronized (mLayers) {
layers.addAll(mLayers);
}
Collections.reverse(layers);
boolean seenTop = false;
for (MapViewLayer l : layers) {
if (l.getType() == LayerType.IMAGERY) {
if (!l.isVisible() || seenTop) {
((MapTilesLayer<?>) l).flushTileCache(null, false);
} else {
seenTop = true;
}
}
}
}

/**
* Return the current (that is top visible) background layer
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ private String sanitize(@NonNull String url) {
Uri.Builder uriBuilder = uri.buildUpon();
uriBuilder.clearQuery();
for (String n : uri.getQueryParameterNames()) {
if (!"request".equalsIgnoreCase(n) && !"service".equalsIgnoreCase(n)) {
if (!"".equals(n) && !"request".equalsIgnoreCase(n) && !"service".equalsIgnoreCase(n)) {
uriBuilder.appendQueryParameter(n, uri.getQueryParameter(n));
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/de/blau/android/views/layers/MapTilesLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.util.BitSet;
import java.util.Collection;
import java.util.Random;

import android.content.ActivityNotFoundException;
import android.content.Context;
Expand Down Expand Up @@ -116,6 +117,8 @@ public class MapTilesLayer<T> extends MapViewLayer implements ExtentInterface, L

private final ViewBox viewBox = new ViewBox();

private final Random random = new Random();

private final TileRenderer<T> mTileRenderer;

// avoid creating new Rects in onDraw
Expand Down Expand Up @@ -388,8 +391,8 @@ protected void onDraw(Canvas c, IMapView osmv) {
Snack.toastTopWarning(ctx, ctx.getString(R.string.toast_tile_layer_errors, myRendererInfo.getName()));
}

long owner = (long) (Math.random() * Long.MAX_VALUE); // unique values so that we can track in the cache which
// invocation of onDraw the tile belongs too
long owner = random.nextLong(); // unique values so that we can track in the cache which
// invocation of onDraw the tile belongs too

int maxZoom = myRendererInfo.getMaxZoomLevel();
int minZoom = myRendererInfo.getMinZoomLevel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public LRUMapTileCache(final long maxCacheSize) {
// ===========================================================

/**
* Overrides clear() to also clear the LRU list.
* Empty all data structures
*/
public synchronized void clear() {
for (CacheElement<T> ce : cache.values()) {
Expand All @@ -123,6 +123,7 @@ public synchronized void clear() {
}
cache.clear();
list.clear();
cacheSize = 0;
}

/**
Expand Down

0 comments on commit 4c07e6d

Please sign in to comment.