The SLM retention clean up task `SnapshotRetentionTask` lists all snapshots in order to identify the snapshots to retain and the snapshots to delete. While doing so it retrieves the full snapshot information, including shard snapshot details and shard snapshot failures, to later  only use snapshot metadata and snapshot timestamp to select the snapshots to retain. When the snapshots contain thousands of shards it represents of lot of objects that are unnecessary created, putting a lot of pressure on the garbage collector.

We should improve the way SLM retrieves snapshots to reduce the huge allocations of objects. David made some [interesting](https://github.com/elastic/elasticsearch/issues/99953#issuecomment-1738534389) [suggestions](https://github.com/elastic/elasticsearch/issues/99953#issuecomment-1738626743) in comments.

- #100044
- #100053
- #100054
- #100058
- #100065
- #100070
- #100092
x-pack/plugin/slm/src/main/java/org/elasticsearch/xpack/slm/SnapshotRetentionTask.java
private final Set<SnapshotId> runningDeletions = Collections.synchronizedSet(new HashSet<>());
    public SnapshotRetentionTask(
        Client client,
        ClusterService clusterService,
        LongSupplier nowNanoSupplier,
        SnapshotHistoryStore historyStore
    ) {
        this.client = new OriginSettingClient(client, ClientHelper.INDEX_LIFECYCLE_ORIGIN);
        this.clusterService = clusterService;
        this.nowNanoSupplier = nowNanoSupplier;
        this.historyStore = historyStore;
    }
    @Override
    public void triggered(SchedulerEngine.Event event) {
        assert event.getJobName().equals(SnapshotRetentionService.SLM_RETENTION_JOB_ID)
            || event.getJobName().equals(SnapshotRetentionService.SLM_RETENTION_MANUAL_JOB_ID)
            : "expected id to be "
                + SnapshotRetentionService.SLM_RETENTION_JOB_ID
                + " or "
                + SnapshotRetentionService.SLM_RETENTION_MANUAL_JOB_ID
                + " but it was "
                + event.getJobName();
        final ClusterState state = clusterService.state();
        
        
        if (SnapshotLifecycleService.slmStoppedOrStopping(state)
            && event.getJobName().equals(SnapshotRetentionService.SLM_RETENTION_MANUAL_JOB_ID) == false) {
            logger.debug("skipping SLM retention as SLM is currently stopped or stopping");
            return;
        }