Solved

Should databinding use weak or hard references?

Posted on 2013-01-07
5
461 Views
Last Modified: 2013-01-09
Should an implementation of a data binding mechanism use WeakReferences or normal hard references on both the source and target objects? In other words, in the data binding context, should it hold the source and target objects as weak or hard references?

Which is the better practice, and why?
0
Comment
Question by:xenonn
  • 3
  • 2
5 Comments
 
LVL 19

Expert Comment

by:Thommy
ID: 38753694
Hard references prevent your objects from garbage collection.
Weak references are eligible for the garbage collector and therefore may return NULL value. Weak references are useful especially for large objects, that can be easily recreated.

This may shed some light on that topics...
what is hard reference in java?
Understanding Weak References
Weak References
0
 

Author Comment

by:xenonn
ID: 38753698
I know what weak and hard references are. I am more interested in whether using weak or hard references is a better practice in a databinding implementation.
0
 
LVL 19

Accepted Solution

by:
Thommy earned 500 total points
ID: 38753831
Normally strong references are stored to both the source and the target of a data binding.
Unless removed manually, data will be hold in memory even when every other reference has been removed and data is no longer required.
Data remains stuck in memory and can cause severe memory leaks.

Therefore to my mind best practice is to use weak-referenced data binding whereever possible to ensure a smaller memory footprint and avoid above described memory leaks.

Especially for large memory-consuming data objects, which are not required all the time during your application run and which can be easily recreated, it is strongly recommend to use weak references...
0
 

Author Comment

by:xenonn
ID: 38759175
You mentioned about recreating the objects which the GC has collected away. Since the GC has already collected away the object, and the data binder has no more reference to the actual initial object, how can the application or the data binder still recreate the already-garbage-collected object with its last lost data?

For example, there is an object of Cat that has the property of Name. We have an instance of Cat with the name of "Kitty":

Cat myCat = new Cat();
myCat.Name = "Kitty";

Open in new window


Suppose the data binder is referencing myCat via a weak reference, and myCat got garbage collected. The data binder wants to recreate the object as you said. The data binder could "new up" another instance of Cat, but it wouldn't know that the new Cat instance it creates need to have its Name property value as "Kitty", ie, the newly recreated object wouldn't be the same as the one it was garbage collected. In this case, wouldn't the newly recreated object be useless because it is a different thing and using it will lead to wrong data?
0
 
LVL 19

Expert Comment

by:Thommy
ID: 38759260
In that case, if you cannot easily recreate your object, you have to use a hard reference.

What I meant is following:
Supposed you have a treeview object that holds all files of a folder,  you can create that treeview when you need it and then release it. If the treeview is required later again, you can easily recreate it...
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is about my first experience with programming Arduino.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question