This discussion is archived
1 Reply Latest reply: Jan 18, 2013 3:33 PM by James_D RSS

Delete cell factories

964914 Newbie
Currently Being Moderated
I have a scene which shows a table with 2 columns. Each columns has its own cell factory class to render the cell value. When the table is rendered, the cell factory classes are created (one for each cell). Now when the user navigates to another scene, how to clear the memory allocated for the cell factory classes. I cleared the column data's , but how to remove each object created for each cell. I tried the following code, but it throws an error saying :
Caused by: java.lang.UnsupportedOperationException

     at com.sun.javafx.collections.UnmodifiableListSet$1.remove(UnmodifiableListSet.java:88)


My code to remove the objects alloted for cell factories:

      Set<Node> colNodes = mytableview.lookupAll("#columnname1");
        
       Iterator<Node> itr = colNodes.iterator();
            while(itr.hasNext()) {
                 Node n = itr.next();
                 colNodes.remove(n);
                //itr.remove();
            }

And same for column 2. but it throws an exception.
Any idea how to clear those objects.
  • 1. Re: Delete cell factories
    James_D Guru
    Currently Being Moderated
    You're getting the exception because the set that's returned is unmodifiable. (Even if it weren't, you'd get a ConcurrentModificationException, as you can't modify a Collection while you're iterating through it.)

    I'm still not convinced you need to worry about doing this at all. You say the "user navigates to another scene". If you mean this literally (i.e., you're changing the scene that's in the stage), then you're calling stage.setScene(someNewScene). At that point, unless you're doing something really weird with your code, there should be no live references to the previous scene, and consequently no live references to the table and all the nodes that are part of the scene graph descending from it. Thus these will be eligible for garbage collection and the garbage collector will get them at some (unspecified) point in the future.

    Even if you're not literally setting the scene, you are presumably removing some part of the scene graph and replacing it with something else. Again, unless you're deliberately hanging on to references to the part of the scene graph that's removed, everything you're worried about will be eligible for garbage collection.

    Do you have some specific reason to convince yourself you need to manually manage memory here? There are certainly cases in Java when you need to do this, but they are (or should be) fairly few and far between.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points