Netbeans DataObject life cycle

Here are some questions which I did not found any answer on the Netbeans wiki:

  • When are DataObject created
  • When are DataObject destroyed
  • Am I expected to store custom data in DataObject

Answers can be found in the openide.loaders/src/org/openide/loaders/ file, in the source code:

  • DataObject are, at least, created when Netbeans need to display a Node representing a FileObject, that is, each time an explorer browse a file system.
  • DataObject are stored as WeakReference in the DataObjectPool instance. They are freed when memory is required.
  • As you do not control when DataObject are created nor disposed you should probably not load nor store data when the DataObject is created. It should be done by actions associated to the DataObject.

There is a funny side efect of using WeakReference to keep track on DataObject. If you open a directory with a lot’s of large DataObject, some of them may be destroyed by the garbage collector as soon are the are created. Then corresponding nodes are not displayed.

#java, #netbeans