java - 随着 tableview 中的列被排序,javafx 中的内存使用量持续增加是否正常?
问题描述
我想知道以下两个观察结果在 JavaFX 中是否正常:
- 我注意到每次对表视图中的列进行排序时,我的 JavaFX 应用程序的内存使用量都会急剧增加(90mb)。(我的数据是一个 ObservableList。Tableview 将有 71000 行和 13 列)
- 对具有双数据类型的列进行排序需要太长时间和太多内存(大约 250mb)。
注意:我使用的是 java 10.0.2
我的表视图代码:
private TableView table = new TableView();
table.setEditable(true);
TableColumn<TableViewWatchlistData, Boolean> colSelected = new TableColumn<>("Selected");
colSelected.setCellFactory(tc ->{return new CheckBoxTableCell();});
colSelected.setEditable(true);
colSelected.setCellValueFactory(new PropertyValueFactory<>("isEntered"));
TableColumn<TableViewWatchlistData, String> colInstToken = new TableColumn<>("Instrument Token");
colInstToken.setCellValueFactory(new PropertyValueFactory<>("instrumentToken"));
TableColumn<TableViewWatchlistData, String> colExngToken = new TableColumn<>("Exchange Token");
colExngToken.setCellValueFactory(new PropertyValueFactory<>("exchangeToken"));
TableColumn<TableViewWatchlistData, String> colTradSymbol = new TableColumn<>("Symbol");
colTradSymbol.setCellValueFactory(new PropertyValueFactory<>("tradingSymbol"));
TableColumn<TableViewWatchlistData, String> colExngName = new TableColumn<>("Exchange");
colExngName.setCellValueFactory(new PropertyValueFactory<>("exchangeName"));
TableColumn<TableViewWatchlistData, String> colCompName = new TableColumn<>("Company Name");
colCompName.setCellValueFactory(new PropertyValueFactory<>("companyName"));
TableColumn<TableViewWatchlistData, Double> colLastPrice = new TableColumn<>("Last Price");
colLastPrice.setCellValueFactory(new PropertyValueFactory<>("lastPrice"));
TableColumn<TableViewWatchlistData, LocalDate> colExpiry = new TableColumn<>("Expiry Date");
colExpiry.setCellValueFactory(new PropertyValueFactory<>("expiry"));
TableColumn<TableViewWatchlistData, Double> colStrike = new TableColumn<>("Strike");
colStrike.setCellValueFactory(new PropertyValueFactory<>("strike"));
TableColumn<TableViewWatchlistData, Double> colTick = new TableColumn<>("Tick");
colTick.setCellValueFactory(new PropertyValueFactory<>("tickSize"));
TableColumn<TableViewWatchlistData, Integer> colLotSize = new TableColumn<>("Lot Size");
colLotSize.setCellValueFactory(new PropertyValueFactory<>("lotSize"));
TableColumn<TableViewWatchlistData, String> colInstType = new TableColumn<>("Instrument Type");
colInstType.setCellValueFactory(new PropertyValueFactory<>("instrumentType"));
TableColumn<TableViewWatchlistData, String> colSegment = new TableColumn<>("Segment");
colSegment.setCellValueFactory(new PropertyValueFactory<>("segment"));
TableColumn<TableViewWatchlistData, LocalDateTime> colLastUpdated = new TableColumn<>("Last Updated");
colLastUpdated.setCellValueFactory(new PropertyValueFactory<>("lastUpdated"));
colSelected.setMinWidth(70);
colInstToken.setMinWidth(120);
colExngToken.setMinWidth(110);
colTradSymbol.setMinWidth(110);
colExngName.setMinWidth(75);
colCompName.setMinWidth(210);
colLastPrice.setMinWidth(75);
colExpiry.setMinWidth(85);
colStrike.setMinWidth(55);
colTick.setMinWidth(45);
colLotSize.setMinWidth(65);
colInstType.setMinWidth(110);
colSegment.setMinWidth(75);
colLastUpdated.setMinWidth(135);
table.getColumns().setAll(colSelected,colInstToken,colExngToken,colTradSymbol,colExngName,colCompName,colLastPrice,colExpiry,colStrike,colTick,colLotSize,colInstType,colSegment,colLastUpdated);
colSelected.setSortType(TableColumn.SortType.DESCENDING);
table.getSortOrder().setAll(colSelected);
colSelected.setSortable(true);
this.kdk.generateTableViewData(watchlistname);
ObservableList<TableViewWatchlistData> items = FXCollections.observableArrayList(this.kdk.watchlistTableViewData.values());
SortedList<TableViewWatchlistData> data = new SortedList<>(items);
data.comparatorProperty().bind(table.comparatorProperty());
table.setItems(data);
table.sort();
}
TableView 数据模型:
import javafx.beans.property.*;
import kite2ami.Instruments;
public class TableViewWatchlistData {
private final SimpleStringProperty instrumentToken = new SimpleStringProperty();
private final SimpleStringProperty exchangeToken = new SimpleStringProperty();
private final SimpleStringProperty tradingSymbol = new SimpleStringProperty();
private final SimpleStringProperty companyName = new SimpleStringProperty();
private final SimpleDoubleProperty lastPrice = new SimpleDoubleProperty();
private final SimpleObjectProperty<LocalDate> expiry = new SimpleObjectProperty<LocalDate>();
private final SimpleDoubleProperty strike = new SimpleDoubleProperty();
private final SimpleDoubleProperty tickSize = new SimpleDoubleProperty();
private final SimpleIntegerProperty lotSize = new SimpleIntegerProperty();
private final SimpleStringProperty instrumentType = new SimpleStringProperty(); //PE, CE, EQ, FUT
private final SimpleStringProperty segment = new SimpleStringProperty();
private final SimpleStringProperty exchangeName = new SimpleStringProperty();
private final SimpleBooleanProperty isEntered = new SimpleBooleanProperty();
private final SimpleObjectProperty<LocalDateTime> lastUpdated = new SimpleObjectProperty<>();
public TableViewWatchlistData(Instruments instrument, boolean isEntered) {
this.setInstrumentToken(instrument.getInstrumentToken());
this.setExchangeToken(instrument.getExchangeToken());
this.setTradingSymbol(instrument.getTradingSymbol());
this.setCompanyName(instrument.getCompanyName());
this.setLastPrice(instrument.getLastPrice());
this.setExpiry(instrument.getExpiry());
this.setStrike(instrument.getStrike());
this.setTickSize(instrument.getTickSize());
this.setLotSize(instrument.getLotSize());
this.setInstrumentType(instrument.getInstrumentType());
this.setSegment(instrument.getSegment());
this.setExchangeName(instrument.getExchangeName());
this.setIsEntered(isEntered);
this.setLastUpdated(instrument.getLastUpdated());
}
public String getInstrumentToken() {
return instrumentToken.get();
}
public SimpleStringProperty instrumentTokenProperty() {
return instrumentToken;
}
public void setInstrumentToken(String instrumentToken) {
this.instrumentToken.set(instrumentToken);
}
public String getExchangeToken() {
return exchangeToken.get();
}
public SimpleStringProperty exchangeTokenProperty() {
return exchangeToken;
}
public void setExchangeToken(String exchangeToken) {
this.exchangeToken.set(exchangeToken);
}
public String getTradingSymbol() {
return tradingSymbol.get();
}
public SimpleStringProperty tradingSymbolProperty() {
return tradingSymbol;
}
public void setTradingSymbol(String tradingSymbol) {
this.tradingSymbol.set(tradingSymbol);
}
public String getCompanyName() {
return companyName.get();
}
public SimpleStringProperty companyNameProperty() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName.set(companyName);
}
public double getLastPrice() {
return lastPrice.get();
}
public SimpleDoubleProperty lastPriceProperty() {
return lastPrice;
}
public void setLastPrice(double lastPrice) {
this.lastPrice.set(lastPrice);
}
public LocalDate getExpiry() {
return expiry.get();
}
public SimpleObjectProperty<LocalDate> expiryProperty() {
return expiry;
}
public void setExpiry(LocalDate expiry) {
this.expiry.set(expiry);
}
public double getStrike() {
return strike.get();
}
public SimpleDoubleProperty strikeProperty() {
return strike;
}
public void setStrike(double strike) {
this.strike.set(strike);
}
public double getTickSize() {
return tickSize.get();
}
public SimpleDoubleProperty tickSizeProperty() {
return tickSize;
}
public void setTickSize(double tickSize) {
this.tickSize.set(tickSize);
}
public int getLotSize() {
return lotSize.get();
}
public SimpleIntegerProperty lotSizeProperty() {
return lotSize;
}
public void setLotSize(int lotSize) {
this.lotSize.set(lotSize);
}
public String getInstrumentType() {
return instrumentType.get();
}
public SimpleStringProperty instrumentTypeProperty() {
return instrumentType;
}
public void setInstrumentType(String instrumentType) {
this.instrumentType.set(instrumentType);
}
public String getSegment() {
return segment.get();
}
public SimpleStringProperty segmentProperty() {
return segment;
}
public void setSegment(String segment) {
this.segment.set(segment);
}
public String getExchangeName() {
return exchangeName.get();
}
public SimpleStringProperty exchangeNameProperty() {
return exchangeName;
}
public void setExchangeName(String exchangeName) {
this.exchangeName.set(exchangeName);
}
public boolean isIsEntered() {
return isEntered.get();
}
public SimpleBooleanProperty isEnteredProperty() {
return isEntered;
}
public void setIsEntered(boolean isEntered) {
this.isEntered.set(isEntered);
}
public LocalDateTime getLastUpdated() {
return lastUpdated.get();
}
public SimpleObjectProperty<LocalDateTime> lastUpdatedProperty() {
return lastUpdated;
}
public void setLastUpdated(LocalDateTime lastUpdated) {
this.lastUpdated.set(lastUpdated);
}
}
解决方案
我设法通过更改以下内容来解决问题:
colSelected.setCellValueFactory(cellData->cellData.getValue().isEnteredProperty());
至
colSelected.setCellValueFactory(new PropertyValueFactory<>("isEntered"));
我仍然不明白引擎盖下发生了什么变化。但它解决了我的问题。如果有人能启发我,那将是你的好意。
推荐阅读
- python - 由于未找到模块“pd.core.dtypes.common”,本地 Ubuntu 机器拒绝导入 Pandas
- java - 来自 textView 的 getIdentifier() 返回 null
- java - 试图理解 Java 中的命名空间是什么
- java - 如何在 Hive 2 中将数据插入到复杂数据类型“Struct”中
- wcf - WCF:无法为具有权限“dev.xxxxx.com”的 SSL/TLS 安全通道建立信任关系
- java - 测量异步方法的执行时间
- reactjs - React 的合成事件与 addEventListener 相比有什么优势吗?
- angular-cli - 无法在 Angular6 中的 `.angular-cli.json` 错误中找到任何应用程序
- php - 从 PHP 中的输入类型文件中获取所选文件的原始路径
- java - 我需要在java中放置两个线程并让我的代码的一部分等待另一部分结束