Skip to content

Instantly share code, notes, and snippets.

@Legioth
Created January 13, 2025 13:20
Show Gist options
  • Select an option

  • Save Legioth/251590634eb7aa6de901776cf88c7857 to your computer and use it in GitHub Desktop.

Select an option

Save Legioth/251590634eb7aa6de901776cf88c7857 to your computer and use it in GitHub Desktop.
Measure Vaadin UI memory consumption by opening lots of tabs
@Bean
public VaadinServiceInitListener memoryMeasure() {
/*-
* 1. Run app in production mode
* 2. Open 100 browser tabs: for i in {1..100} ; do open "http://localhost:8080/"; done
* 3. Verify UI count and wait until the memory measurement in the console has stabilized
* 4. Close all the tabs
* 5. Wait until new memory measurement has stabilized
*/
AtomicInteger openUIs = new AtomicInteger();
return serviceInit -> {
VaadinService service = serviceInit.getSource();
service.addUIInitListener(uiInit -> {
openUIs.incrementAndGet();
uiInit.getUI().addDetachListener(detach -> {
openUIs.decrementAndGet();
});
});
Thread thread = new Thread(() -> {
try {
Runtime runtime = Runtime.getRuntime();
int maxUis = 0;
long minMemoryAfterOpening = Long.MAX_VALUE;
long minMemoryAfterClosing = Long.MAX_VALUE;
while (true) {
long usedMemory = runtime.totalMemory()
- runtime.freeMemory();
int uis = openUIs.get();
if (uis == 0 && maxUis > 0) {
if (usedMemory < minMemoryAfterClosing
&& usedMemory < minMemoryAfterOpening) {
System.out.println(
"New record for no UIs: " + usedMemory);
minMemoryAfterClosing = usedMemory;
long diff = minMemoryAfterOpening
- minMemoryAfterClosing;
System.out.println("Diff: " + diff);
System.out.println("Diff per UI: "
+ diff / (double) maxUis);
}
} else if (uis > maxUis) {
maxUis = uis;
minMemoryAfterOpening = usedMemory;
} else if (uis != 0 && uis == maxUis) {
if (usedMemory < minMemoryAfterOpening) {
System.out.println("New record for " + uis
+ " UIs: " + usedMemory);
minMemoryAfterOpening = usedMemory;
}
}
runtime.gc();
Thread.sleep(1000);
}
} catch (InterruptedException e) {
return;
}
});
thread.start();
service.addServiceDestroyListener(destory -> thread.interrupt());
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment