Solved

Should databinding use weak or hard references?

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

830 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