From 988ce0587ef3e0d1e2e32fc34dfe6e5b83ce9693 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Tue, 6 Nov 2012 00:36:12 +0100 Subject: [PATCH] Memory limitation --- .../src/net/osmand/router/routing.xml | 3 +- OsmAnd/jni/Application.mk | 3 +- .../osmand/plus/routing/RouteProvider.java | 2 + .../osmand/src/binaryRoutePlanner.cpp | 7 +- Osmand-kernel/osmand/src/binaryRoutePlanner.h | 66 ++++++++++++++++++- 5 files changed, 74 insertions(+), 7 deletions(-) diff --git a/DataExtractionOSM/src/net/osmand/router/routing.xml b/DataExtractionOSM/src/net/osmand/router/routing.xml index 9b86e94d9e..1c97fd54f5 100644 --- a/DataExtractionOSM/src/net/osmand/router/routing.xml +++ b/DataExtractionOSM/src/net/osmand/router/routing.xml @@ -10,7 +10,8 @@ - + + diff --git a/OsmAnd/jni/Application.mk b/OsmAnd/jni/Application.mk index fdd99b5789..571ed52b96 100755 --- a/OsmAnd/jni/Application.mk +++ b/OsmAnd/jni/Application.mk @@ -18,5 +18,6 @@ endif ifndef OSMAND_DEBUG_NATIVE # Force release compilation in release optimizations, even if application is debuggable by manifest -APP_OPTIM := release +#APP_OPTIM := release +APP_OPTIM := debug endif \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/routing/RouteProvider.java b/OsmAnd/src/net/osmand/plus/routing/RouteProvider.java index fd5e05ea5b..b5261788e8 100644 --- a/OsmAnd/src/net/osmand/plus/routing/RouteProvider.java +++ b/OsmAnd/src/net/osmand/plus/routing/RouteProvider.java @@ -360,6 +360,8 @@ public class RouteProvider { memoryLimit, specialization); if(!params.optimal){ cf.heuristicCoefficient *= 1.5; + // native use + cf.attributes.put("heuristicCoefficient", cf.heuristicCoefficient+""); } RoutingContext ctx = new RoutingContext(cf, NativeOsmandLibrary.getLoadedLibrary(), files); ctx.interruptable = params.interruptable; diff --git a/Osmand-kernel/osmand/src/binaryRoutePlanner.cpp b/Osmand-kernel/osmand/src/binaryRoutePlanner.cpp index 28d4a68109..2463791ebe 100644 --- a/Osmand-kernel/osmand/src/binaryRoutePlanner.cpp +++ b/Osmand-kernel/osmand/src/binaryRoutePlanner.cpp @@ -1,6 +1,5 @@ #include "common.h" #include -#include #include "binaryRead.h" #include "binaryRoutePlanner.h" #include @@ -670,4 +669,8 @@ vector searchRouteInternal(RoutingContext* ctx, bool leftSid return res; } - +bool compareRoutingSubregionTile(SHARED_PTR o1, SHARED_PTR o2){ + int v1 = (o1->access + 1) * pow((float)10, o1->getUnloadCount() -1); + int v2 = (o2->access + 1) * pow((float)10, o2->getUnloadCount() -1); + return v1 < v2 ; +} diff --git a/Osmand-kernel/osmand/src/binaryRoutePlanner.h b/Osmand-kernel/osmand/src/binaryRoutePlanner.h index 32f8c8dfba..7dfb413813 100644 --- a/Osmand-kernel/osmand/src/binaryRoutePlanner.h +++ b/Osmand-kernel/osmand/src/binaryRoutePlanner.h @@ -2,6 +2,7 @@ #define _OSMAND_BINARY_ROUTE_PLANNER_H #include "common.h" #include "binaryRead.h" +#include typedef UNORDERED(map) MAP_STR_FLOAT; typedef UNORDERED(map) MAP_STR_STR; @@ -76,7 +77,17 @@ struct RoutingSubregionTile { } void setLoaded(){ - loaded++; + loaded = abs(loaded) + 1; + } + + void unload(){ + routes.clear(); + size = 0; + loaded = - abs(loaded); + } + + int getUnloadCount() { + return abs(loaded); } int getSize(){ @@ -170,14 +181,15 @@ struct RoutingConfiguration { minDefaultSpeed = parseFloat("minDefaultSpeed", 45) / 3.6; maxDefaultSpeed = parseFloat("maxDefaultSpeed", 130) / 3.6; heurCoefficient = parseFloat("heuristicCoefficient", 1); - memoryLimitation = (int)parseFloat("memoryLimitInMB", memoryLimitation); + // don't use file limitations? + memoryLimitation = (int)parseFloat("nativeMemoryLimitInMB", memoryLimitation); zoomToLoad = (int)parseFloat("zoomToLoadTiles", 16); routerName = parseString("name", "default"); routerProfile = parseString("baseProfile", "car"); distanceRecalculate = parseFloat("recalculateDistanceHelp", 10000) ; } - RoutingConfiguration(vector& config, float initDirection = -360, int memLimit = 30) : + RoutingConfiguration(vector& config, float initDirection = -360, int memLimit = 100) : memoryLimitation(memLimit), initialDirection(initDirection) { for(int j = 0; j o1, SHARED_PTR o2); + struct RoutingContext { typedef UNORDERED(map) > MAP_SUBREGION_TILES; @@ -360,6 +374,8 @@ struct RoutingContext { int64_t firstRoadId; RoutingConfiguration config; + int gcCollectIterations; + int startX; int startY; int endX; @@ -390,8 +406,52 @@ struct RoutingContext { return sz; } + void unloadUnusedTiles(int memoryLimit) { + int sz = getSize(); + float critical = 0.9f * memoryLimit * 1024 * 1024; + if(sz < critical) { + return; + } + float occupiedBefore = sz / (1024. * 1024.); + float desirableSize = memoryLimit * 0.7f * 1024 * 1024; + vector > list; + MAP_SUBREGION_TILES::iterator it = subregionTiles.begin(); + int loaded = 0; + int unloadedTiles = 0; + for(;it != subregionTiles.end(); it++) { + if(it->second->isLoaded()) { + list.push_back(it->second); + loaded++; + } + } + sort(list.begin(), list.end(), compareRoutingSubregionTile); + int i =0; + while(sz >= desirableSize && i < list.size()) { + SHARED_PTR unload = list[i]; + i++; + sz -= unload->getSize(); + unload->unload(); + unloadedTiles ++; + } + for(i = 0; iaccess /= 3; + } + osmand_log_print(LOG_DEBUG, "Run GC (before %f Mb after %f Mb) unload %d of %d tiles", + occupiedBefore, getSize() / (1024.0*1024.0), + unloadedTiles, loaded); + } + void loadHeaderObjects(int64_t tileId) { vector > subregions = indexedSubregions[tileId]; + bool gc = false; + for(int j = 0; jisLoaded()) { + gc = true; + } + } + if(gc) { + unloadUnusedTiles(config.memoryLimitation); + } for(int j = 0; jisLoaded()) { loadedTiles++;