修复内存泄漏 (#1888)

* Fix memory leaks

* Cache icon in cell

* Lazy acquisition of availableCharsets
This commit is contained in:
Glavo 2022-12-02 17:27:52 +08:00 committed by GitHub
parent ea91ee91ec
commit a6231ac593
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 19 deletions

View File

@ -141,10 +141,16 @@ public final class Controllers {
}
public static void onApplicationStop() {
config().setHeight(stageHeight.get());
config().setWidth(stageWidth.get());
stageHeight = null;
stageWidth = null;
if (stageHeight != null) {
config().setHeight(stageHeight.get());
stageHeight.unbind();
stageHeight = null;
}
if (stageWidth != null) {
config().setWidth(stageWidth.get());
stageWidth.unbind();
stageWidth = null;
}
}
public static void initialize(Stage stage) {
@ -321,5 +327,6 @@ public final class Controllers {
decorator = null;
stage = null;
scene = null;
onApplicationStop();
}
}

View File

@ -127,7 +127,8 @@ public final class VersionsPage extends BorderPane implements WizardPage, Refres
btnRefresh.setGraphic(wrap(SVG.refresh(Theme.blackFillBinding(), -1, -1)));
MutableObject<RemoteVersionListCell> lastCell = new MutableObject<>();
list.setCellFactory(listView -> new RemoteVersionListCell(lastCell));
EnumMap<VersionIconType, Image> icons = new EnumMap<>(VersionIconType.class);
list.setCellFactory(listView -> new RemoteVersionListCell(lastCell, icons));
list.setOnMouseClicked(e -> {
if (list.getSelectionModel().getSelectedIndex() < 0)
@ -218,29 +219,26 @@ public final class VersionsPage extends BorderPane implements WizardPage, Refres
}
private static class RemoteVersionListCell extends ListCell<RemoteVersion> {
private static final EnumMap<VersionIconType, Image> icon = new EnumMap<>(VersionIconType.class);
private static Image getIcon(VersionIconType type) {
assert Platform.isFxApplicationThread();
return icon.computeIfAbsent(type, iconType -> new Image(iconType.getResourceUrl(), 32, 32, false, true));
}
final IconedTwoLineListItem content = new IconedTwoLineListItem();
final RipplerContainer ripplerContainer = new RipplerContainer(content);
final StackPane pane = new StackPane();
private final MutableObject<RemoteVersionListCell> lastCell;
private final EnumMap<VersionIconType, Image> icons;
RemoteVersionListCell(MutableObject<RemoteVersionListCell> lastCell) {
RemoteVersionListCell(MutableObject<RemoteVersionListCell> lastCell, EnumMap<VersionIconType, Image> icons) {
this.lastCell = lastCell;
}
this.icons = icons;
{
pane.getStyleClass().add("md-list-cell");
StackPane.setMargin(content, new Insets(10, 16, 10, 16));
pane.getChildren().setAll(ripplerContainer);
}
private Image getIcon(VersionIconType type) {
return icons.computeIfAbsent(type, iconType -> new Image(iconType.getResourceUrl(), 32, 32, false, true));
}
@Override
public void updateItem(RemoteVersion remoteVersion, boolean empty) {
super.updateItem(remoteVersion, empty);

View File

@ -88,7 +88,7 @@ public final class CompressingUtils {
}
public static Charset findSuitableEncoding(Path zipFile) throws IOException {
return findSuitableEncoding(zipFile, Charset.availableCharsets().values());
return findSuitableEncoding(zipFile, null);
}
public static Charset findSuitableEncoding(Path zipFile, Collection<Charset> candidates) throws IOException {
@ -98,7 +98,7 @@ public final class CompressingUtils {
}
public static Charset findSuitableEncoding(ZipFile zipFile) throws IOException {
return findSuitableEncoding(zipFile, Charset.availableCharsets().values());
return findSuitableEncoding(zipFile, null);
}
public static Charset findSuitableEncoding(ZipFile zipFile, Collection<Charset> candidates) throws IOException {
@ -106,6 +106,9 @@ public final class CompressingUtils {
if (OperatingSystem.NATIVE_CHARSET != StandardCharsets.UTF_8 && testEncoding(zipFile, OperatingSystem.NATIVE_CHARSET))
return OperatingSystem.NATIVE_CHARSET;
if (candidates == null)
candidates = Charset.availableCharsets().values();
for (Charset charset : candidates)
if (charset != null && testEncoding(zipFile, charset))
return charset;
@ -156,8 +159,6 @@ public final class CompressingUtils {
public FileSystem build() throws IOException {
if (autoDetectEncoding) {
if (!testEncoding(zip, encoding)) {
if (charsetCandidates == null)
charsetCandidates = Charset.availableCharsets().values();
encoding = findSuitableEncoding(zip, charsetCandidates);
}
}