Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MemoryLeak while using CachedViewModelCellFactory in ListView #603

Open
NoaNoob opened this issue Oct 21, 2019 · 0 comments
Open

MemoryLeak while using CachedViewModelCellFactory in ListView #603

NoaNoob opened this issue Oct 21, 2019 · 0 comments

Comments

@NoaNoob
Copy link

NoaNoob commented Oct 21, 2019

Hi,
I'm using a ListView and implemented a DetailView using the CachedViewModelCellFactory. While running the app over a while I noticed that the cache of the CellFactory is not cleared when new Items are loaded and the app is consuming a growing amount of memory.
As a workaround I accessed the cache via reflections an cleared it manually:

public class MyFancyView implements Initializable, FxmlView<MyFancyViewModel> {

    @FXML
    ListView<MyResultViewModel> listMyResultViewModels;

    ViewListCellFactory<MyResultViewModel> cellFactory;
    
    @Override
    public void initialize(URL location, ResourceBundle resources) {

            cellFactory =
                    CachedViewModelCellFactory.createForFxmlView(MyResultView.class);
            listMyResultViewModels.setCellFactory(cellFactory);

     }

   // call this method on demand
    private void clearCellFactoryCache() {

        try {

            Field cache = cellFactory.getClass().getDeclaredField("cache");
            cache.setAccessible(true);
            Map<MyResultViewModel, ViewTuple<MyResultView, MyResultViewModel>> cachedVMs = (HashMap) cache.get(cellFactory);

            cachedVMs.clear();

        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

}

Is there an alternative way to do this? Maybe a "cachedSetItems(List vms)" Method?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant