Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Do trees and tables hold on to references to data?  RSS feed

 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My SWING application has a JTree and a JTable. The tree nodes have a File as their user data, while the table cells have a File or a String as their data.
My table, particularly, is constantly changing and I often remove rows from it. I had hoped that this would cause the associated data to get garbage collected, as I am not holding on to it any other way (I hope), but this does not seem to happen (looking in JProbe). Am I supposed to be doing something else?
My table has a DefaultTableModel as its model and I use DefaultTableModel.removeRow() to remove the rows I no longer want.
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter,
Garbage collection (GC) is not guaranteed to run at all during your program life. It might run in low memory situations or whenever it feels like it. You can however, hint to it about starting with the System.gc() method. This still won't gaurantee that it will run.
Unless you are running out of memory while you code is executing I wouldn't worry to much about when the GC executes and your memory returns.
By using the Model you are correctly using the Swing architecture and there is nothing more you can do about the GC.
Regards,
Manfred.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With JProbe, there is an "invoke GC" button, which I believe does a full GC. I think JProbe hooks into the JVM in a way that an ordinary application cannot. I think my data is hanging around even after this.
As you say that I am not making a mistake with my use of Swing, I should probably look elsewhere for something that might be retaining references when it shouldn't.
Thanks.
P.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!