You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConnectorTrackerMemoryLeakUI.java 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package com.vaadin.tests.components.ui;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import com.vaadin.server.VaadinRequest;
  5. import com.vaadin.ui.Button;
  6. import com.vaadin.ui.CssLayout;
  7. import com.vaadin.ui.Label;
  8. import com.vaadin.ui.UI;
  9. import com.vaadin.ui.VerticalLayout;
  10. import com.vaadin.ui.Window;
  11. public class ConnectorTrackerMemoryLeakUI extends UI {
  12. public static final String BUTTON_CAPTION = "Kill!";
  13. public static final String LABEL_STOPPED = "Still alive!";
  14. private CssLayout panel = new CssLayout();
  15. private List<String> items = new ArrayList<>(200);
  16. private VerticalLayout layout = new VerticalLayout();
  17. @Override
  18. protected void init(VaadinRequest vaadinRequest) {
  19. Button button = new Button(BUTTON_CAPTION);
  20. button.addClickListener(event -> {
  21. gc();
  22. long memory = Runtime.getRuntime().totalMemory();
  23. System.out.println("Before: " + memory);
  24. // To simulate 200 concurrent session we do this 200 times
  25. for (int i = 0; i < 200; i++) {
  26. // Clear items
  27. items.clear();
  28. for (int j = 1; j <= 200; j++) {
  29. // Add one item and update the panel with those
  30. items.add("Item #" + j);
  31. updatePanel(panel, items);
  32. }
  33. }
  34. // We made it this far. Good for us.
  35. Label labelStop = new Label(LABEL_STOPPED);
  36. layout.addComponent(labelStop);
  37. gc();
  38. long delta = Runtime.getRuntime().totalMemory() - memory;
  39. memory = memory + delta;
  40. System.out.println(" After: " + memory + " (+" + delta + ")");
  41. });
  42. layout.addComponents(button, panel);
  43. setContent(layout);
  44. }
  45. private void gc() {
  46. // Sometimes the VM needs a couple of "suggestions" to actually
  47. // perform gc. There is no automated test for this UI so tweak if
  48. // needed.
  49. for (int i = 0; i < 3; i++) {
  50. Runtime.getRuntime().gc();
  51. }
  52. try {
  53. Thread.sleep(500);
  54. } catch (InterruptedException e1) {
  55. }
  56. }
  57. private static void updatePanel(CssLayout panel, List<String> items) {
  58. panel.removeAllComponents();
  59. items.forEach(i -> panel.addComponent(new Button(i, event -> {
  60. Window w = new Window();
  61. UI.getCurrent().addWindow(w);
  62. })));
  63. }
  64. }