Solved

Should databinding use weak or hard references?

Posted on 2013-01-07
5
464 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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 fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

627 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