Skip to main content

Posts

Showing posts from July, 2007

Java reference types - an example and questions

Dr. Heinz Kabutz has an excellent example for Soft, Weak, and Phantom references in one of his newsletters. You can view it here . I also created mind maps for the Java garbage collector and Java reference types. You can download the images by clicking on the links below: Java garbage collector Reference types in Java    Before concluding the series on Reference types in Java, here are some simple questions for you to test your knowledge. Q1. Consider the following code: String s = new String("abc"); SoftReference sf = new SoftReference(s);  What is the reference type (strong or soft) for "abc" ? Q2. What is the main difference between phantom  references and the other (soft and weak) two references? Q3. Consider the following code: SoftReference sf = new SoftReference("abc");  WeakReference wr = new WeakReference("123");  Which of the two strings will be garbage collected first, when the JVM needs memory?   Note: This text was originall

Reference queue

The garbage collector puts Reference objects in the ReferenceQueue after their referents have been reclaimed (Phantom references are an exception, because they continue to retain a link to their referents even after they are placed on teh ReferenceQueue). In most cases when Soft and Weak references are used, we do not care to monitor the ReferenceQueue. However, in case of PhantomReferences, we do want to monitor the ReferenceQueue, because they are used when we wish to do some cleanup activities when the reference is reclaimed. In such cases, a thread which monitors the ReferenceQueue to find out which objects have been reclaimed will be effective. The WeakHashMap class in the JDK however, follows a slightly different strategy as explained here . While Soft and Weak references can be created without being associated with a ReferenceQueue, Phantom references must always be associated with a ReferenceQueue. The java.lang.ref.ReferenceQueue class gives us three methods to interact

Phantom references

Phantom references are the weakest of the three reference types. There is however a significant difference between Phantom references and the other reference types. Objects that are referenced by phantom references are put on the reference queue before the memory occupied by their referents is reclaimed, while soft and weak references are enqueued after the memory occupied by their referent is reclaimed. Another important difference in the words of Ethan Nicholas is; "A phantom reference is quite different than either SoftReference or WeakReference. Its grip on its object is so tenuous that you can't even retrieve the object -- its get() method always returns null. The only use for such a reference is keeping track of when it gets enqueued into a ReferenceQueue, as at that point you know the object to which it pointed is dead. How is that different from WeakReference, though?" So what good are Phantom References? Well according to the documentation : Phantom refer

Weak references

A weak reference is similar to a soft reference, except that objects that are weakly reachable will be garbage collected before objects that are softly reachable. When the JVM needs memory, it will first reclaim all unused objects, if the reclaimed memory is still not sufficient, then all weakly referenced objects will will be garbage collected, and if it needs even more memory, then it will reclaim memory from softly referenced objects.   WeakReference wr = new WeakReference(new String(“abc”));       ReferenceQueue rq = new ReferenceQueue(); WeakReference wr = new WeakReference(new PhantomReference(new String(“abc”), queue));       String s = new String(“abc”); ReferenceQueue queue = new ReferenceQueue(); WeakReference wr = new WeakReference(s); PhantomReference pr = new PhantomReference(s, queue); s = null;     All of the above are examples of weak references. In the above example we see two new concepts; phantom reference, and refere

Soft references

If an object is not referenced by by a strong reference and there is at least one link to that object with a soft reference, then the object is a softly referenced object. Java provides us a class, which is a subclass of java.lang.ref.Reference, called SoftReference. Soft references are created by encapsulating the object which we want to refer, inside a SoftReference object. Some examples of soft references are shown below. SoftReference sf = new SoftReference(new String(“abc”));   SoftReference sf = new SoftReference(new WeakReference(new String(“abc”))); String s = new String(“abc”); SoftReference sf = new SoftReference(s); WeakReference wr = new WeakReference(s); s = null; In these examples we see another type of reference called a weak reference. The next section talks about weak references. Note: This text was originally posted on my earlier blog at http://www.adaptivelearningonline.net

New series - reference types in Java

Starting this week, I will post a series of blogs on "Reference Types In Java". So here goes... Since version 1.2, Java supports references other than the commonly used strong reference . An object is said to be strongly referenced, when at least one path to the object from the root set of references contains a strong reference. A strong reference is the traditional type of reference which is created by the = operator. The garbage collector considers a strongly referenced object to be in use and will not reclaim it. This statement creates a strong reference. String s = new String("abc");  The String object holding the value “abc” is strongly referenced because the variable 's' directly refers to it. Java 1.2 supports three more types of references; SoftReference, WeakReference, and PhantomReference. Whenever we want an object to be softly, weakly, or phantomly referenced, we do not directly reference the object using an '=' operator. We instead

A process for teaching a peer taught online course

Sometime back I wrote a blog on the motivation for conducting online, peer taught courses, and the kind of infrastructure such a course would need. Once we have the basic infrastructure in place, it may be useful to define a simple process or a set of guidelines for students as well as mentors to help them as they proceed with the course. It is very important to explain the learning methodology and it's benefits to the students and mentors. The students must understand right at the beginning that learning that is spoon fed is the most ineffective way of learning. The core process that they are to follow is learning by doing, and participating with other students, mentors, and practitioners. Traditional lectures would also be needed, but their purpose should be to introduce students to the topics and show them how to build on the knowledge by practicing and participating. The mentors should interact with the students through lectures, online groups, and blogs. The participation

A process for teaching a peer taught, online course - contd

Once all the grading and other process details are explained to the students, the faculty starts by explaining the first topic in a kickstart fashion and giving a set of resources to the students for further understanding. So, for example, if the topic is “Input/Output in Java”, the faculty will explain the basic architecture of the IO library, they will demonstrate a simple program that reads input from a console and writes to the console. The lecture ends with an introduction of the full capabilities of the library and a list of resources which the students must study to further their understanding. If possible a topic may also be linked to a project milestone. The students will then proceed with their learning by reading the link or maybe viewing screencasts, or listening to podcasts. They should write their learning and perspectives on their blog, and ask doubts on he newsgroup. The mentors will follow the students blogs and help them with their understanding by commenting on the b