Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Frequent GeoJSONSource updates force GC every couple seconds #14048

Closed

Description

@tobrun

With looking into global reference overflow issue from #14010, I have been profiling different components in the SDK and noticing that using LocationComponent results in gc'ing every couple seconds with the SimpleLocationActivity while not interacting:

image

image

Activity

  1. added
    Android
    on Mar 6, 2019
  2. tobrun commented on Mar 6, 2019

    @tobrun
    MemberAuthor

    Setting locationComponent.setRenderMode(RenderMode.NORMAL) doesn't show the GC'ing:

    image

    We need to optimize RenderMode.COMPASS

  3. tobrun commented on Mar 6, 2019

    @tobrun
    MemberAuthor

    LocationComponentCompassEngine#updateRotation seems to be source.
    Tracking down which part concretely.

    update: when we receive a sensor change event, we are recreating all animators with updateCompassAnimators and calling playAnimators inside of feedNewCompassBearing this is the source of the gc thrashing.

    update2: was able to pinpoint that the setGeoJson is the large part of the memory thrashing and not the recreating/playing of the animators.

  4. tobrun commented on Mar 14, 2019

    @tobrun
    MemberAuthor

    With using:

      mapView.setMaximumFps(30);
      locationComponent.setMaxAnimationFps(15);
    

    I'm getting better results:

    image

    This can be used as intermediate solution until we tackled the root cause of this issue.

  5. LukasPaczos commented on Apr 9, 2019

    @LukasPaczos
    Contributor

    I've noticed, that all other objects ignored/commented out, it's the constant detaching and attaching of the JVM env to different threads that invokes GC runs. Here's a PR that tries to minimize that for the GeoJSONSource's async features conversion - #14375. I can see a great decrease in the GC runs, however, a lot of them are still coming from other places, like the MapRenderer.

    Because when animating (for example LocationComponent or the AnimatedSymbolLayerActivity) we are generating new tiles and rendering really quickly, and because the MapRenderer uses a shared thread pool, the renderer is the main source of the attach/detach calls. Should we make a fixed thread pool of non-rotating threads for the renderer as well?

    All above is based on my experimental discovery that attaching and detaching threads is the root cause, I'm not sure if there's anything deeper in our JNI wrapper that might help decrease GC runs instead.

    /cc @ivovandongen

  6. changed the title [-]Simple Location Activity gc'ing every couple seconds[/-] [+]Frequent GeoJSONSource updates force GC every couple seconds[/+] on Apr 9, 2019
  7. LukasPaczos commented on Apr 12, 2019

    @LukasPaczos
    Contributor

    Capturing from @ivovandongen in #14375:

    Attaching a thread to the JVM indeed forces a sync with the GC (which may or may not require the GC to run).

    Following that lead, I noticed that shared thread pool's threads are not manually attached to the JVM (in contrast to the run loop's implementation). This means that they always fall into JNI_EDETACHED branch:

    case JNI_EDETACHED:
    return UniqueEnv(jni::AttachCurrentThread(*theJVM).release(), JNIEnvDeleter(*theJVM, true));

    while calling android::AttachEnv() and are detached as soon as the variable goes out of the scope because of the true flag passed to the JNIEnvDeleter.

    To get rid of the unnecessary GC runs we need to introduce an Android's shared thread pool implementation that manually attaches/detaches the threads to JVM when they are created/destroyed and rely on android::AttachEnv() only for getting the env reference.

  8. added this to the release-oolong milestone on May 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Android

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions