Solved

Should databinding use weak or hard references?

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

708 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

14 Experts available now in Live!

Get 1:1 Help Now