Skip to content

Garbage collection is not aggressive enough as memory usage gets higher. #42078

Closed
@dnfield

Description

@dnfield

Flutter allocates images in such a way that the native memory is held on to as long as there are any references to the underlying SkImage. These images are typically held as GPU memory, and Skia has no functionality to clean up the memory until all references are dropped.

It has been observed in flutter/flutter#56482 that a forcing a garbage collection (e.g. via the observatory or by calling Dart_NotifyLowMemory) frees up the resources after the image has passed into the old gen correctly. However, the image can sit in the old gen for many many frames (seconds), and while allocating another large image will trigger a GC, there can be overlap between the allocation of the new image memory and the GC such that time passes where both images are still resident in GPU memory. This can be a problem on devices with lower memory when sufficiently large images are allocated.

A Flutter program can dispose an image when it doesn't want to use it anymore, but other garbage-collectable objects may still hold references to that image (e.g. SkPicture/Picture objects). While one option is to track down all the native references and try to clear them, that can become brittle and is basically re-implementing a subset of work that the GC would be doing for a GC. Instead, we'd like to have some API to tell the GC that doing a full collection now is believed to result in significant native memory reduction, and have the GC run when we dispose the image.

One suggestion offline was to implement some method to tell the VM memory size has changed - that's probably a good thing to have as well, but unfortunately wouldn't help us in this case, since to change the native memory allocation we would have to find all references and unref them as long as they're garbage.

/cc @rmacnak-google @mraleph @bkonyi
/cc @jason-simmons @chinmaygarde

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-vmUse area-vm for VM related issues, including code coverage, and the AOT and JIT backends.customer-fluttervm-native

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions