Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
0
of 0 vote

(As the link is not allowed to be pasted, here is the explanation):

A weak reference, simply put, is a reference that isn't strong enough to force an object to remain in memory. Weak references allow you to leverage the garbage collector's ability to determine reachability for you, so you don't have to do it yourself. You create a weak reference like this:

WeakReference<Widget> weakWidget = new WeakReference<Widget>(widget);

- msingh March 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

(As the link is not allowed to be pasted, here is the explanation):

A weak reference, simply put, is a reference that isn't strong enough to force an object to remain in memory. Weak references allow you to leverage the garbage collector's ability to determine reachability for you, so you don't have to do it yourself. You create a weak reference like this:

WeakReference<Widget> weakWidget = new WeakReference<Widget>(widget);

- msingh March 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

a weak reference is a reference that does not protect the referent object from collection by a garbage collector. An object referenced only by weak references is considered unreachable (or "weakly reachable") and so may be collected at any time. Weak references are used to avoid keeping memory referenced by unneeded objects. Some garbage-collected languages feature or support various levels of weak references, such as Java, C#, Python, Perl or Lisp.

Garbage collection is used to reduce the potential for memory leaks and data corruption. There are two main types of garbage collection: tracing and reference counting. Reference counting schemes record the number of references to a given object and collect the object when the reference count becomes zero. Reference-counting cannot collect cyclic (or circular) references because only one object may be collected at a time. Groups of mutually referencing objects which are not directly referenced by other objects and are unreachable can thus become permanently resident; if an application continually generates such unreachable groups of unreachable objects this will have the effect of a memory leak. Weak references may be used to solve the problem of circular references if the reference cycles are avoided by using weak references for some of the references within the group.
Weak references are also used to minimize the number of unnecessary objects in memory by allowing the program to indicate which objects are not critical by only weakly referencing them.



Java Example
If you create a weak reference and then elsewhere in the code you can use get() to get the actual object, the weak reference isn't strong enough to prevent garbage collection, so you may find (if there are no strong references to the object) that get() suddenly starts returning null. [1]
import java.lang.ref.WeakReference;

public class ReferenceTest {
public static void main(String[] args) throws InterruptedException {

Student s1 = new Student(1);
System.out.println(s1);
WeakReference<Student> ws = new WeakReference<Student>(s1);
System.out.println(ws.get());
s1 = null;
System.gc();
Thread.sleep(1000);

System.out.println(ws.get());
}
}

class Student {
public Student(int id) {
this.id = id;
}

int id;

public String toString() {

return "[id=" + id + "]";

}
}
Another use of weak references is in writing a cache. Using, for example, a weak hash map, one can store in the cache the various referred objects via a weak reference. When the garbage collector runs — when for example the application's memory usage gets sufficiently high — those cached objects which are no longer directly referenced by other objects are removed from the cache.

- Sal July 06, 2010 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More