Skip to content

fix: Release no longer visible items from memory - #22185

Merged
tltv merged 4 commits into
mainfrom
remove-no-longer-visible-items-from-cache
Sep 8, 2025
Merged

tltv merged 4 commits into
mainfrom
remove-no-longer-visible-items-from-cache

Conversation

@vursen

@vursen vursen commented Sep 8, 2025 •

Copy link
Copy Markdown
Contributor

Description

The PR ensures that items are removed from the HierarchicalDataCommunicator's cache and properly disposed of in both KeyMapper and DataGenerator once they move out of the viewport. Without this, such features as ComponentRenderer can cause high memory usage on the client side because the Flow client continues to retain references to DOM elements that are no longer visible.

Regression from #21876

Type of change

  • Bugfix

@github-actions

github-actions Bot commented Sep 8, 2025 •

Copy link
Copy Markdown
Contributor

Test Results

1 247 files  + 1  1 247 suites  +1   1h 15m 55s ⏱️ +51s
8 635 tests + 4  8 568 ✅ + 4  67 💤 ±0  0 ❌ ±0 
9 050 runs  +12  8 972 ✅ +11  78 💤 +1  0 ❌ ±0 

Results for commit 130fd8b. ± Comparison against base commit 0d6d1d0.

This pull request removes 3 and adds 7 tests. Note that renamed tests count towards both.
com.vaadin.flow.data.provider.hierarchy.HierarchicalDataCommunicatorBasicTest ‑ collapseItems_collapsedChildrenRemovedFromKeyMapper
com.vaadin.flow.data.provider.hierarchy.HierarchicalDataCommunicatorBasicTest ‑ collapseItems_dataGeneratorDestroyDataCalledForCollapsedChildren
com.vaadin.flow.data.provider.hierarchy.HierarchicalDataCommunicatorBasicTest ‑ getDepth_returnsDepthForCachedItemsAfterTheyAreLoaded
com.vaadin.flow.data.provider.hierarchy.HierarchicalDataCommunicatorBasicTest ‑ getDepth_returnsDepthForViewportItems
com.vaadin.flow.data.provider.hierarchy.HierarchicalDataCommunicatorDataGenerationTest ‑ changeViewportRangeBackAndForth_destroyDataCalledForNoLongerVisibleItems
com.vaadin.flow.data.provider.hierarchy.HierarchicalDataCommunicatorDataGenerationTest ‑ changeViewportRangeBackAndForth_generateDataCalledForVisibleItems
com.vaadin.flow.data.provider.hierarchy.HierarchicalDataCommunicatorDataGenerationTest ‑ collapseItems_destroyDataCalledBeforeItemRemovedFromKeyMapper
com.vaadin.flow.data.provider.hierarchy.HierarchicalDataCommunicatorDataGenerationTest ‑ collapseItems_destroyDataCalledForCollapsedChildrenInKeyMapper
com.vaadin.flow.data.provider.hierarchy.HierarchicalDataCommunicatorKeyGenerationTest ‑ changeViewportRangeBackAndForth_noLongerVisibleKeysRemoved
com.vaadin.flow.data.provider.hierarchy.HierarchicalDataCommunicatorKeyGenerationTest ‑ collapseItems_collapsedKeysRemoved

♻️ This comment has been updated with latest results.

Comment on lines +786 to +787
dataGenerator.destroyData(item);
getKeyMapper().remove(item);

@vursen vursen Sep 8, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The call order is important. ComponentRenderer assumes that the item key is still present when destroyData is called. If the key is missing, the component won't be detached from the DOM and will cause memory to build up on the client side.

// Clear items that are no longer visible from the cache to free memory.
// This also removes them from keyMapper and disposes of their data
// generated by dataGenerator.
rootCache.removeDescendantItemIf((item) -> getKeyMapper().has(item)

@vursen vursen Sep 8, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking getKeyMapper().has(item) makes sure to only remove items that were actually rendered, leaving any other cached items untouched.

@vursen
vursen marked this pull request as ready for review September 8, 2025 10:55
@vursen vursen changed the title fix: release no longer visible items from memory fix: Release no longer visible items from memory Sep 8, 2025
@mshabarov
mshabarov requested a review from tltv September 8, 2025 11:34
@sonarqubecloud

sonarqubecloud Bot commented Sep 8, 2025

Copy link
Copy Markdown

}

@Test
public void collapseItems_collapsedChildrenRemovedFromKeyMapper() {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: this test was moved to HierarchicalDataCommunicatorKeyGenerationTest.

}

@Test
public void collapseItems_dataGeneratorDestroyDataCalledForCollapsedChildren() {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: this test was moved to the new HierarchicalDataCommunicatorDataGenerationTest.

@tltv
tltv merged commit 9daf03f into main Sep 8, 2025
@tltv
tltv deleted the remove-no-longer-visible-items-from-cache branch September 8, 2025 13:40
Copilot AI pushed a commit that referenced this pull request Oct 1, 2025
This change ensures that items are removed from the HierarchicalDataCommunicator's cache and properly disposed of in both KeyMapper and DataGenerator once they move out of the viewport. Without this, such features as ComponentRenderer can cause high memory usage on the client side because the Flow client continues to retain references to DOM elements that are no longer visible.

Related-to: #21876
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants