StreamRegistration help with unregister

So I am just opening a pdf file in a new tab.

private void help() {
		try {
			File file = new File(AppHelper.getJndiProperty("help.file"));
			ByteArrayInputStream bais = new ByteArrayInputStream(FileUtils.readFileToByteArray(file));
			
			StreamResource resource = new StreamResource("help.pdf", () -> bais);
			
			final StreamRegistration registration = VaadinSession.getCurrent().getResourceRegistry()
					.registerResource(resource);
			
			UI.getCurrent().getPage().open(registration.getResourceUri().toString());
		} catch (Exception e) {
			logger.error("USER: {} -> help -> FAILED: {}",user.userName,e.getMessage());
			ErrorNotification errorNotification = new ErrorNotification("CONTACT SUPPORT - Open Help File Failed: " + e.getMessage());
			errorNotification.open();
		}
	}

The javadoc for registerResource states:

Registers a stream resource in the session and returns registration handler.

You can get resource URI to use it in the application (e.g. set an attribute value or property value) via the registration handler. The registration handler should be used to unregister resource when it's not needed anymore. Note that it is the developer's responsibility to unregister resources. Otherwise resources won't be garbage collected until the session expires which causes memory leak.

Parameters:
resource stream resource to register
Returns:
registration handler

So any suggestions on how to properly unregister resource?

This seems like the best solution I can find online. Anyone see an issue doing this or is there a better way?

protected void onDetach(DetachEvent detachEvent) {
		super.onDetach(detachEvent);
		if (registration != null) {
			registration.unregister();
		}
	}

If you have multiple stream resources registered, you might want to hold a collection of them and unregister them in onDetach method. This approach with onDetach looks good, I see no other solution which can unregister it earlier that detaching the component/UI.