Move update cache size to AsyncLoadingThread

This commit is contained in:
Vitaliy 2021-04-12 18:15:22 +03:00
parent b1ba628987
commit a2b6a24e5c
2 changed files with 26 additions and 20 deletions

View file

@ -7,6 +7,7 @@ import net.osmand.data.RotatedTileBox;
import net.osmand.map.ITileSource; import net.osmand.map.ITileSource;
import net.osmand.map.MapTileDownloader.DownloadRequest; import net.osmand.map.MapTileDownloader.DownloadRequest;
import net.osmand.plus.SQLiteTileSource; import net.osmand.plus.SQLiteTileSource;
import net.osmand.plus.views.MapTileLayer;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -15,6 +16,7 @@ import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Map.Entry;
import java.util.Stack; import java.util.Stack;
/** /**
@ -38,6 +40,7 @@ public class AsyncLoadingThread extends Thread {
public void run() { public void run() {
while (true) { while (true) {
try { try {
updateBitmapTilesCache();
boolean tileLoaded = false; boolean tileLoaded = false;
boolean mapLoaded = false; boolean mapLoaded = false;
while (!requests.isEmpty()) { while (!requests.isEmpty()) {
@ -69,6 +72,24 @@ public class AsyncLoadingThread extends Thread {
} }
} }
private void updateBitmapTilesCache() {
int maxCacheSize = 0;
for (Entry<MapTileLayer, Integer> entry : resourceManger.getMapTileLayerSizes().entrySet()) {
MapTileLayer layer = entry.getKey();
if (layer.isVisible()) {
maxCacheSize += entry.getValue();
}
}
BitmapTilesCache bitmapTilesCache = resourceManger.getBitmapTilesCache();
if (maxCacheSize > 0 && maxCacheSize != bitmapTilesCache.getMaxCacheSize()) {
long freeMemory = Runtime.getRuntime().freeMemory() / (1024 * 1024L);
if ((freeMemory > 0 || maxCacheSize < bitmapTilesCache.getMaxCacheSize())) {
log.info("Bitmap tiles to load in memory : " + maxCacheSize);
bitmapTilesCache.setMaxCacheSize(maxCacheSize);
}
}
}
public void requestToLoadTile(TileLoadDownloadRequest req) { public void requestToLoadTile(TileLoadDownloadRequest req) {
requests.push(req); requests.push(req);
} }

View file

@ -90,7 +90,6 @@ public class ResourceManager {
public static final String VECTOR_MAP = "#vector_map"; //$NON-NLS-1$ public static final String VECTOR_MAP = "#vector_map"; //$NON-NLS-1$
private static final String INDEXES_CACHE = "ind.cache"; private static final String INDEXES_CACHE = "ind.cache";
public static final String DEFAULT_WIKIVOYAGE_TRAVEL_OBF = "Default_wikivoyage.travel.obf"; public static final String DEFAULT_WIKIVOYAGE_TRAVEL_OBF = "Default_wikivoyage.travel.obf";
private static final int CACHE_SIZE_UPDATE_INTERVAL_MS = 10 * 1000;
private static final Log log = PlatformUtil.getLog(ResourceManager.class); private static final Log log = PlatformUtil.getLog(ResourceManager.class);
@ -101,8 +100,7 @@ public class ResourceManager {
private List<TilesCache> tilesCacheList = new ArrayList<>(); private List<TilesCache> tilesCacheList = new ArrayList<>();
private BitmapTilesCache bitmapTilesCache; private BitmapTilesCache bitmapTilesCache;
private GeometryTilesCache geometryTilesCache; private GeometryTilesCache geometryTilesCache;
private Map<MapTileLayer, Integer> mapTileLayerSizes = new HashMap<>(); private Map<MapTileLayer, Integer> mapTileLayerSizes = new ConcurrentHashMap<>();
private long lastCacheSizeUpdateTime;
private final OsmandApplication context; private final OsmandApplication context;
private List<ResourceListener> resourceListeners = new ArrayList<>(); private List<ResourceListener> resourceListeners = new ArrayList<>();
@ -290,29 +288,16 @@ public class ResourceManager {
resourceListeners.remove(listener); resourceListeners.remove(listener);
} }
public Map<MapTileLayer, Integer> getMapTileLayerSizes() {
return mapTileLayerSizes;
}
public void setMapTileLayerSizes(MapTileLayer layer, int tiles) { public void setMapTileLayerSizes(MapTileLayer layer, int tiles) {
mapTileLayerSizes.put(layer, tiles); mapTileLayerSizes.put(layer, tiles);
updateBitmapTilesCache();
} }
public void removeMapTileLayerSize(MapTileLayer layer) { public void removeMapTileLayerSize(MapTileLayer layer) {
mapTileLayerSizes.remove(layer); mapTileLayerSizes.remove(layer);
updateBitmapTilesCache();
}
private void updateBitmapTilesCache() {
if (System.currentTimeMillis() - lastCacheSizeUpdateTime > CACHE_SIZE_UPDATE_INTERVAL_MS) {
lastCacheSizeUpdateTime = System.currentTimeMillis();
int maxCacheSize = 0;
for (Integer layerTiles : mapTileLayerSizes.values()) {
maxCacheSize += layerTiles;
}
long freeMemory = Runtime.getRuntime().freeMemory() / (1024 * 1024L);
if (maxCacheSize != bitmapTilesCache.getMaxCacheSize() && (freeMemory > 0 || maxCacheSize < bitmapTilesCache.getMaxCacheSize())) {
log.info("Bitmap tiles to load in memory : " + maxCacheSize);
bitmapTilesCache.setMaxCacheSize(maxCacheSize);
}
}
} }
public void resetStoreDirectory() { public void resetStoreDirectory() {