Solved

Should databinding use weak or hard references?

Posted on 2013-01-07
5
459 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now