Optimize CPU and memory usage
This guide shows how to reduce CPU use, memory use, and map storage in an Android app that uses the HERE SDK. It lists settings for common use cases, gives code examples, and explains how to measure the results.
Start by turning off features your app does not need. A lower frame rate can reduce the work needed to draw the map. Smaller memory caches reduce RAM use. Downloading less map data and using a smaller disk cache mainly save storage space and network data.
Note
Navigation and offline maps require a Navigate license.
LayerConfigurationonly works with a Navigate license that includes offline maps. Check that your HERE SDK version supports the APIs you plan to use.
Choose settings for your use case
First measure the app with its current settings. Then change one setting and repeat the same actions on the same device. Compare the results before and after the change. CPU and graphics processor (GPU) use show processing load; frames per second (FPS) describe the frame rate.
| Use case | What to change and check | What to consider |
|---|---|---|
| Online map browsing without turn-by-turn guidance | Set enabledFeatures (data available for downloads) and implicitlyPrefetchedFeatures (data fetched ahead while showing the map) to include only LayerConfiguration.Feature.RENDERING, the basic map-drawing data. You can find the example here. Measure the effect of this change separately. To check a lower frame rate, first measure CPU/GPU use while panning and zooming at the current rate. Then call mapView.setFrameRate(30) and repeat the same movements. Compare CPU/GPU use and check that movement is still smooth enough. | Navigation and offline search/routing data are left out of downloads and prefetching. The SDK can still download visual data when needed online. A lower frame rate makes movement less smooth. |
| Online search or route planning without a map | Use SearchEngine and RoutingEngine without creating a MapView. | Avoids the work and memory needed to draw a map. These engines need an internet connection. They do not use installed regions or cached map data when the connection is lost. |
| Turn-by-turn guidance with a map | Keep LayerConfiguration.Feature.NAVIGATION enabled and use a camera behavior. First measure CPU/GPU use while following a route at the current guidance frame rate. Then call visualNavigator.setGuidanceFrameRate(20) and repeat the route. Compare CPU/GPU use, and check that camera movement and upcoming turns remain easy to follow. | Reduces map drawing work, not guidance processing. Do not change MapView's frame rate while VisualNavigator is rendering. |
| Guidance without a map | Use Navigator, or call VisualNavigator.stopRendering() when you no longer need navigation visuals. | Stops drawing navigation visuals. Guidance and location processing continue. Stop them separately when no longer needed. |
| A simpler map, with or without guidance | Use MapScene.disableFeatures() to turn off unneeded MapFeatures.EXTRUDED_BUILDINGS, LANDMARKS, SHADOWS, and AMBIENT_OCCLUSION. Turn off TRAFFIC_FLOW and TRAFFIC_INCIDENTS if you do not need traffic on the map. | Shows less detail and reduces drawing work. Turning off traffic on the map also stops its tile requests. Routes can still take traffic into account. Apply these settings again after loading another map scene. |
| Offline car navigation | Keep LayerConfiguration.Feature.RENDERING, LayerConfiguration.Feature.NAVIGATION, and LayerConfiguration.Feature.OFFLINE_ROUTING enabled. Add LayerConfiguration.Feature.OFFLINE_SEARCH only if needed. Use MapDownloader to install only the required regions. See the offline configuration. | Saves map storage, but does not necessarily reduce RAM use. Add LayerConfiguration.Feature.TRUCK for offline truck routing and map matching, or LayerConfiguration.Feature.OFFLINE_BUS_ROUTING for bus routes. |
| Occasional online browsing with little storage space | Keep the default cache size. The Engines topic recommends at least 256 MB, which is also the default, so do not use a smaller value such as 128 MB for this experiment. Avoid installing offline regions unless the app needs offline use. | Installed regions use separate storage from the map cache. The cache uses least-recently-used eviction when full. |
| Repeated travel in the same area | With the current cache size, follow a trip twice and record downloads and loading time on the second trip. In a separate app run, set SDKOptions.cacheSizeInBytes to a larger value, such as 512 MB, before SDK initialization. Repeat both trips and compare the second-trip results. Install regions if the app must work offline. | Uses more disk space to keep more data for reuse. Cached data can still be removed. Installed regions are stored separately and are not removed when the cache is full. |
| Devices with little RAM | First record memory use and response times with the current settings. In a separate app run, set SDKOptions.lowMemoryMode = true before SDK initialization. Repeat the same actions and compare the results. Separately, if Android reports low memory, call SDKNativeEngine.purgeMemoryCaches(SDKNativeEngine.PurgeMemoryStrategy.FULL). | Smaller memory caches reduce performance. Clearing them frees cache memory, not all SDK memory. Later operations must load the data again. |
The example cache sizes and frame rates are not suitable for every device. Keep a setting only if it reduces resource use and the app still works well on your supported devices. See map features, navigation without a map, offline maps, and map caching for more details.
Example configurations
These examples show settings you can combine in your app. To measure the effect of each setting, apply one change at a time. For settings in SDKOptions, start a separate app run with the new options rather than replacing the engine while the app is in use.
Online-only map browsing
Use this configuration if your app shows a map and uses SearchEngine and RoutingEngine, but does not need guidance or offline search and routing. It selects only basic map drawing data and leaves the cache size at its default. Set these SDKOptions before creating the shared engine, a MapView, or other SDK engines. The Java examples use java.util.Arrays.
LayerConfiguration layers = new LayerConfiguration();
layers.enabledFeatures = Arrays.asList(LayerConfiguration.Feature.RENDERING);
layers.implicitlyPrefetchedFeatures = Arrays.asList(LayerConfiguration.Feature.RENDERING);
SDKOptions options = new SDKOptions(
AuthenticationMode.withKeySecret(accessKeyID, accessKeySecret));
options.layerConfiguration = layers;val layers = LayerConfiguration()
layers.enabledFeatures = listOf(LayerConfiguration.Feature.RENDERING)
layers.implicitlyPrefetchedFeatures = listOf(LayerConfiguration.Feature.RENDERING)
val options = SDKOptions(
AuthenticationMode.withKeySecret(accessKeyID, accessKeySecret)
)
options.layerConfiguration = layersPass options to SDKNativeEngine.makeSharedInstance(context, options) in your initialization code, and handle InstantiationErrorException there. Do not replace the shared engine just to switch between map browsing and guidance. Replacing it destroys the previous engine. You must then recreate the objects that used that engine.
The enabledFeatures selects data for region downloads. implicitlyPrefetchedFeatures selects data for prefetching: downloading data in advance, before it is needed. This includes downloads made while showing the map, and downloads started with RoutePrefetcher or PolygonPrefetcher. Its default list includes LayerConfiguration.Feature.NAVIGATION. Changing only enabledFeatures leaves navigation prefetching enabled. Note that implicitlyPrefetchedFeatures is a beta API, so there could be a few bugs and unexpected behaviors.
This example leaves out LayerConfiguration.Feature.NAVIGATION, LayerConfiguration.Feature.OFFLINE_SEARCH, LayerConfiguration.Feature.OFFLINE_ROUTING, LayerConfiguration.Feature.DETAIL_RENDERING, and LayerConfiguration.Feature.LANDMARKS_3D, among other features. The SDK can still download data to draw buildings and landmarks when needed online. To hide these features too, use the map drawing settings below.
The configuration applies to all regions. You cannot set it for one region only. Changing it does not immediately remove cached or installed data. For existing data, follow Update the layer configuration. Use MapScene.disableFeatures() to hide map features, not LayerConfiguration.
Offline car navigation
For offline car guidance without offline place search or 3D landmarks, use the following layer selection instead of the online-only selection above. Set it before SDK initialization. Keep a valid map cache even when regions are installed. Navigation still needs the cache.
LayerConfiguration layers = new LayerConfiguration();
layers.enabledFeatures = Arrays.asList(
LayerConfiguration.Feature.RENDERING,
LayerConfiguration.Feature.NAVIGATION,
LayerConfiguration.Feature.OFFLINE_ROUTING);
layers.implicitlyPrefetchedFeatures = Arrays.asList(LayerConfiguration.Feature.NAVIGATION);
options.layerConfiguration = layers;val layers = LayerConfiguration()
layers.enabledFeatures = listOf(
LayerConfiguration.Feature.RENDERING,
LayerConfiguration.Feature.NAVIGATION,
LayerConfiguration.Feature.OFFLINE_ROUTING
)
layers.implicitlyPrefetchedFeatures = listOf(LayerConfiguration.Feature.NAVIGATION)
options.layerConfiguration = layersDownload the required regions with MapDownloader before going offline. Use OfflineRoutingEngine to calculate routes without an internet connection. Add LayerConfiguration.Feature.OFFLINE_SEARCH to enabledFeatures if you use OfflineSearchEngine with installed regions. Also add it to implicitlyPrefetchedFeatures if search must use prefetched data outside those regions. If you need detailed buildings or 3D landmarks offline, add LayerConfiguration.Feature.DETAIL_RENDERING or LayerConfiguration.Feature.LANDMARKS_3D, respectively. Otherwise, you can hide these map features as shown below.
Each feature selects a group of map data layers. Some features share layers. For example, LayerConfiguration.Feature.NAVIGATION includes the Optimized Client Map (OCM) layer groups interop, rendering, navigation, and routing. LayerConfiguration.Feature.OFFLINE_ROUTING includes the same groups plus car_offline_routing. Turning off one feature does not remove data that another enabled feature needs. See the LayerConfiguration.Feature API Reference for the layer groups and data needed for each transport mode.
Reduce map drawing work
After the map scene loads successfully, turn off map features that the app does not need. These settings change what the map shows, not which offline data is downloaded. Apply them again after loading a different scene.
mapView.getMapScene().disableFeatures(Arrays.asList(
MapFeatures.EXTRUDED_BUILDINGS,
MapFeatures.LANDMARKS,
MapFeatures.SHADOWS,
MapFeatures.AMBIENT_OCCLUSION,
MapFeatures.TRAFFIC_FLOW,
MapFeatures.TRAFFIC_INCIDENTS));
mapView.setFrameRate(30);mapView.mapScene.disableFeatures(listOf(
MapFeatures.EXTRUDED_BUILDINGS,
MapFeatures.LANDMARKS,
MapFeatures.SHADOWS,
MapFeatures.AMBIENT_OCCLUSION,
MapFeatures.TRAFFIC_FLOW,
MapFeatures.TRAFFIC_INCIDENTS
))
mapView.frameRate = 30For guidance, set the frame rate on VisualNavigator as shown below. Do not change MapView's frame rate while VisualNavigator is rendering. This example requires a camera behavior, such as the default behavior that follows the user's position:
visualNavigator.setGuidanceFrameRate(20);
visualNavigator.startRendering(mapView);visualNavigator.guidanceFrameRate = 20
visualNavigator.startRendering(mapView)If your app does not set a frame rate, MapView uses a maximum of 60 FPS and guidance uses a maximum of 30 FPS. No call is needed to select these defaults. The examples above explicitly change those limits to 30 FPS for map browsing and 20 FPS for guidance. When measuring the effect, compare with the rate your app used before the change, which may differ from the default. The actual frame rate can be lower than the limit.
Without a camera behavior, VisualNavigator uses the map view's frame rate from before startRendering(). It does not use the guidance frame rate in that case. stopRendering() restores the original map frame rate. Setting MapView's frame rate to 0 stops automatic map drawing. Do not use 0 for a map that must update as the user interacts with it.
See Adjust the frame rate and Customize the navigation experience. When the map is hidden, follow the MapView lifecycle and stop drawing what is no longer needed. When guidance ends, also stop location updates and other work that the app no longer needs. stopRendering() alone does not stop guidance or destroy the MapView.
Handle low memory
See Handling low memory states for Android callbacks and code examples. Clear memory caches only when needed: loading their data again can slow the app for a short time. Clearing the disk map cache is not a way to reduce RAM use.
Measure the effect
- Measure before changing a setting. Use a real device that your app supports. For timing measurements, use a release build with profiling enabled. Keep the route, map movements, map scheme, camera settings, and location input the same in each run. Use
LocationSimulatorto repeat a navigation trip without driving. Run the app long enough to see whether it slows down as the device heats up. - Use a profiler to find the cause. Use Android Studio CPU profiling to find which threads use CPU time. Use a system trace to check how long frames take to draw. Use the Memory Profiler to measure both Java/Kotlin and native memory. The HERE SDK uses both. Some memory recordings need a debuggable build. Do not compare timing from that build directly with release-build timing.
- Change one setting and measure again. Compare the new results with those recorded in step 1. For frame rates or map features, compare CPU/GPU use and frame times. Pan, zoom, or follow the same route, and check that movement is smooth enough and turns remain easy to follow. For
lowMemoryMode, compare memory use and response times after the same amount of activity in each run. For map data or disk cache settings, compare storage use, downloaded bytes, and map loading time on repeat visits. For each configuration, measure once with an empty cache and again with data already cached. Compare empty-cache runs with each other, and cached-data runs with each other. Use the same map data version and installed regions for each comparison. - Check that required features still work. If the app must work offline, turn off the internet connection and check routing, search, map matching, and map appearance. Open and close the map several times. Check whether memory keeps growing because old
ActivityorMapViewobjects remain in memory. Also check that background work stops when it is no longer needed.
Updated 7 hours ago