Consider the below snippet.
import java.util.Map;
import java.util.WeakHashMap;
public class TestWeakHeakHashMap
{
private String name = new String("java");
private Map cache = new WeakHashMap<String, ComplexDO>();
public void testMethod()
{
cache.put(name, new ComplexDO("1", name));
//Discard the strong reference to the key
name = null;
while (true) {
System.gc();
/**
* Verify Full GC with the -verbose:gc option
Since there is no strong reference to the key, it is assumed that the
entry has been removed from the WeakHashMap
*/
System.out.println(cache.size());
}
}
private class ComplexDO
{
private String id;
private String name;
public ComplexDO(String id, String name)
{
this.id = id;
this.name = name;
}
}
}
Now when the testMethod() is run what do you expect the output to be? Since the strong reference to key is discarded, we assume that the entry from the map would be removed, and map would be empty after a full GC.
But that does not happen though.
Let us see what was the put operation on the WeakHashMap.
cache.put(name, new ComplexDO("1", name));
Here the value ComplexDO was holding the key name. This would mean that the value always strongly refers to the key, and hence the key would never be garbage collected. The entry would always remain the map.This is what WeakHashMap API says - "The value objects in a WeakHashMap are held by ordinary strong references. Thus care should be taken to ensure that value objects do not strongly refer to their own keys, either directly or indirectly, since that will prevent the keys from being discarded."
18 comments:
This is great info to know.
die Ausgezeichnete Antwort, ist wacker:) levitra vs viagra dosage levitra preis [url=http//t7-isis.org]cialis preis[/url]
used car melbourne fl
used car bozeman
used car kooskia mt
ricart used car factory
car hire online
pimp your car online
custom your car online
custom bluid your car online
It was amazing to receive many good info in this survey. To improve the business it's suggested to use software outsourcing development to appear on IT markets. Software development allows to automate business processes and gain more profits.
Java SE WeakHashMap Courses An Observation Training Java Training Institutes in Chennai Java SE Course WeakHashMap has only one entry Training whose key refers Java Training in Chennai Java SE & Java EE Courses wrapper over HashMap and the HashMap's Training Java Online Course WeakHashMap/HashMap does not replace the earlier key Training Java SE Java EE Training
We are No 1 Android Training Institute in Chennai, and our trainers are well experienced in software development over 10+ years of experience.
Android Training in Chennai |
Android Training in Chennai |
Android Training in Chennai |
Android Training in Chennai |
by giving the moneylender an every day level of their visa and ace card deals until the point when the payback is finished. Cash Advances corona
Only few shipper loan banks don't require the dealer to change their charge card processors organization. Most time this won't be an issue at all since the rates will be coordinated.
Online Payday Loan corona
Home Mart is a site about Home Improvement, Furniture, Home Appliances and many more.
Check out the best
furniture sale
Dog Cages
bedroom furniture nz
entertainment unit
shoe rack
Thank you much more for sharing the valuable post with a brief explanation and very easy to understand the content for all readers. These further details are helpful for freshers and well do.
Placement Training in Chennai
Training institutes in Chennai with placement
Oracle DBA Training in Chennai
Social Media Marketing Courses in Chennai
Pega Training in Chennai
Job Openings in Chennai
Oracle Training in Chennai
Primavera Training in Chennai
Unix Training in Chennai
Placement Training in Vadapalani
Placement Training in Tambaram
valuable blog,Informative content...thanks for sharing, Waiting for the next update...
SAS Training in Chennai
SAS Institute in Chennai
SAS Training Chennai
SAS Training in Velachery
SAS Training in Tambaram
clinical sas training in chennai
Mobile Testing Training in Chennai
QTP Training in Chennai
Hibernate Training in Chennai
DOT NET Training in Chennai
Wonderful Blog.... Thanks for sharing with us...
Hadoop Training in Chennai
Big data training in chennai
big data course
Hadoop Course in Chennai
Big data training in vadapalani
Hadoop training in porur
Python Training in Chennai
JAVA Training in Chennai
Selenium Training in Chennai
Software testing training in chennai
very nice post...
inplant training in chennai
inplant training in chennai
inplant training in chennai for it
Australia hosting
mexico web hosting
moldova web hosting
albania web hosting
andorra hosting
australia web hosting
denmark web hosting
I like your post very much. I need more updates from your side. It will be very useful for my future reference and further research.
Home & Living
Excellent post, From this post i got more detailed information,
Thanks to share with us,
sap training in chennai
sap training in porur
azure training in chennai
azure training in porur
cyber security course in chennai
cyber security course in porur
ethical hacking course in chennai
ethical hacking course in porur
It seems you are so busy in last month. The detail you shared about your work and it is really impressive that's why i am waiting for your post because i get the new ideas over here and you really write so well.
angular js training in chennai
angular js training in tambaram
full stack training in chennai
full stack training in tambaram
php training in chennai
php training in tambaram
photoshop training in chennai
photoshop training in tambaram
Thanks for sharing this unique and informative content which provided me the required information.
web designing training in chennai
web designing training in velachery
digital marketing training in chennai
digital marketing training in velachery
rpa training in chennai
rpa training in velachery
tally training in chennai
tally training in velachery
I like your post very much. I need more updates from your side.
hardware and networking training in chennai
hardware and networking training in annanagar
xamarin training in chennai
xamarin training in annanagar
ios training in chennai
ios training in annanagar
iot training in chennai
iot training in annanagar
Post a Comment