Skip to content

Cleanup causing a memory leak with <ScriptGoogleMapsAdvancedMarkerElement> #646

@Donitron

Description

@Donitron

🐛 The bug

Using a simple setup with the Google Maps SFC components I found out that the markers do not get cleaned resulting in detached markers.

<ScriptGoogleMaps
    :apiKey="$config.public.google.mapsApiKey"
    :center="center"
    :mapOptions="{ zoom: zoom }"
    :class="['h-full w-full', { 'overflow-hidden rounded-2xl': isSingle }]"
    aboveTheFold
    trigger="immediate"
    width="100%"
    height="100%"
  >
    <ScriptGoogleMapsAdvancedMarkerElement
      v-for="location in locations"
      :key="location.id"
      :options="{
        position: {
          lat: location.latitude,
          lng: location.longitude
        }
      }">
    </ScriptGoogleMapsAdvancedMarkerElement>
  </ScriptGoogleMaps>
Image

In the memory profiler I was able to confirm that the memory stayed about the same when leaving the page with the map component. When navigating back and forth to the maps page and the home page it kept stacking up, eventually leading to a heap out of memory crash.
Snapshot 24 is a snapshot on the maps page and snapshot 25 is on the home page with a forced garbage collection.

My assumption is that the creation of the markers is async and doesn't work well with the component unmount lifecycle. So the following would happen:

  1. Component mounts and starts async process to create marker.
  2. Component unmounts because of a re-render for example. Although I was able to trigger this on load as well. Marker does not exist yet so cleanup does nothing.
  3. The marker gets created and added to the map after unmounting.
  4. No component owns it anymore and won't be cleaned up.

🛠️ To reproduce

See above, could not get an example to load google maps

🌈 Expected behavior

The markers are properly cleaned up instead of becoming detached elements.

ℹ️ Additional context

Using Nuxt 4.3.1 and Nuxt Scripts 1.0.0-beta.19

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions