-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Prevents potential memory leaks caused by UIs being retained by the preserve on refresh cache when the browser page is closed.
- Loading branch information
1 parent
81940be
commit c0d6e07
Showing
9 changed files
with
580 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
flow-tests/test-misc/src/main/java/com/vaadin/flow/misc/ui/ActiveUIsView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright 2000-2024 Vaadin Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package com.vaadin.flow.misc.ui; | ||
|
||
import com.vaadin.flow.component.ComponentUtil; | ||
import com.vaadin.flow.component.UI; | ||
import com.vaadin.flow.component.html.Div; | ||
import com.vaadin.flow.component.html.H1; | ||
import com.vaadin.flow.component.html.NativeButton; | ||
import com.vaadin.flow.router.Route; | ||
|
||
@Route("active-uis") | ||
public class ActiveUIsView extends Div { | ||
|
||
public ActiveUIsView() { | ||
Div uis = new Div(); | ||
uis.setId("uis"); | ||
NativeButton listUIsButton = new NativeButton("List active UIs", | ||
event -> { | ||
UI current = UI.getCurrent(); | ||
listUIs(current, uis); | ||
}); | ||
listUIsButton.setId("list-uis"); | ||
|
||
Div gcCollectedUIs = new Div(); | ||
gcCollectedUIs.setId("gcuis"); | ||
NativeButton listGCCollectedUIsButton = new NativeButton( | ||
"List GC collected UIs", event -> { | ||
listGCCollectedUIs(gcCollectedUIs); | ||
}); | ||
listGCCollectedUIsButton.setId("list-gc-collected-uis"); | ||
NativeButton gcHintButton = new NativeButton("Run GC", | ||
event -> System.gc()); | ||
gcHintButton.setId("gc-hint"); | ||
|
||
add(listUIsButton, new H1("Active UIs (excluding current)"), uis, | ||
listGCCollectedUIsButton, gcHintButton, | ||
new H1("GC collected UIs"), gcCollectedUIs); | ||
} | ||
|
||
private void listGCCollectedUIs(Div gcCollectedUIs) { | ||
gcCollectedUIs.removeAll(); | ||
UI ui = UI.getCurrent(); | ||
ComponentUtil.getData(ui, UITrackerListener.UITracker.class) | ||
.getCollectedUIs(ui.getSession()).forEach(uiId -> gcCollectedUIs | ||
.add(makeDiv("GC Collected UI: " + uiId))); | ||
} | ||
|
||
private void listUIs(UI currentUI, Div uis) { | ||
uis.removeAll(); | ||
currentUI.getSession().getUIs().stream().filter(ui -> ui != currentUI) | ||
.map(ui -> makeDiv("UI: " + ui.getUIId() + ", Path: " | ||
+ ui.getInternals().getActiveViewLocation().getPath())) | ||
.forEach(uis::add); | ||
} | ||
|
||
private static Div makeDiv(String text) { | ||
Div div = new Div(); | ||
div.setText(text); | ||
return div; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
flow-tests/test-misc/src/main/java/com/vaadin/flow/misc/ui/CustomServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright 2000-2024 Vaadin Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package com.vaadin.flow.misc.ui; | ||
|
||
import javax.servlet.annotation.WebInitParam; | ||
import javax.servlet.annotation.WebServlet; | ||
|
||
import com.vaadin.flow.server.VaadinServlet; | ||
|
||
@WebServlet(urlPatterns = "/*", asyncSupported = true, initParams = { | ||
@WebInitParam(name = "heartbeatInterval", value = "5") }) | ||
public class CustomServlet extends VaadinServlet { | ||
public static long HEARTBEAT_INTERVAL = 5; | ||
} |
53 changes: 53 additions & 0 deletions
53
flow-tests/test-misc/src/main/java/com/vaadin/flow/misc/ui/PreserveOnRefreshView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright 2000-2024 Vaadin Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package com.vaadin.flow.misc.ui; | ||
|
||
import com.vaadin.flow.component.UI; | ||
import com.vaadin.flow.component.html.Div; | ||
import com.vaadin.flow.component.html.H1; | ||
import com.vaadin.flow.component.html.H3; | ||
import com.vaadin.flow.component.html.NativeButton; | ||
import com.vaadin.flow.router.AfterNavigationEvent; | ||
import com.vaadin.flow.router.AfterNavigationObserver; | ||
import com.vaadin.flow.router.PreserveOnRefresh; | ||
import com.vaadin.flow.router.Route; | ||
|
||
@PreserveOnRefresh | ||
@Route("preserve") | ||
public class PreserveOnRefreshView extends Div | ||
implements AfterNavigationObserver { | ||
|
||
private final Div uiId; | ||
|
||
public PreserveOnRefreshView() { | ||
uiId = new Div(); | ||
uiId.setId("uiId"); | ||
NativeButton reloadButton = new NativeButton("Reload page", | ||
ev -> UI.getCurrent().getPage().reload()); | ||
reloadButton.setId("reload"); | ||
add(new H1("This view is preserved on refresh")); | ||
H3 initialUIId = new H3("Initial UI: " + UI.getCurrent().getUIId()); | ||
initialUIId.setId("initialUIId"); | ||
add(initialUIId); | ||
add(uiId, reloadButton); | ||
} | ||
|
||
@Override | ||
public void afterNavigation(AfterNavigationEvent event) { | ||
uiId.setText("UI: " + UI.getCurrent().getUIId()); | ||
} | ||
} |
Oops, something went wrong.