Apple Interview Question for Software Engineer / Developers






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

*Garbage Collection - JAVA*
During execution of program in JVM, Objects are created in JVM Heap and variables (Local or Static) that hold the reference to these objects will be residing in JVM Stack. Such variables in the stack are called root object, as they are the root object to directly refer to one object at heap and other objects referenced indirectly can be accessed through these root objects.
An object residing in Heap can not be accessed if no variables (residing in JVM stack) are present to hold reference to them and such Objects are called Garbage, as they occupy memory but are not usable.
There are methods to Identify and Clean such objects:

1. Reference Count: Objects those are not referenced (directly and indirectly) are identified and removed. Failed to identify cyclic garbage objects.
Finding circularity in a Single Linked List is easy but in reality, the objects in a heap forms n-ary tree, where an object (a node) can have references to many objects (many children nodes). In such structures (a tree by definition can not have circularity, so it becomes a graph which can have cyclic structure), finding circularity is very difficult (need to find out if there exists any algorithm for this). Thus we need a different approach for Garbage Collection.

2. Mark and Sweep: Objects those are accessible (directly and indirectly) from root objects are marked first. Then scan through all objects in the heap and remove unmarked objects.
An extra variable can be used for storing mark/unmark information per object. This method eliminates cyclic garbage but creates fragmentations of Heap. The normal program execution is suspended while the garbage collection algorithm runs which makes the program less responsive.

3. Stop and Copy: Marked or Accessible objects are copied to contiguous memory location in a reserved area of the Heap and then current heap is cleaned completely. Reserved area will be used as active heap and cleaned area will be reserved for next garbage collection.
Even though it overcomes fragmentation issue, the program needs to be halted to execute garbage collecting algorithm like before. It also requires double the heap memory size to accommodate reserve heap.

4. Mark and Compact: Marked or Accessible objects are copied to same heap in a contiguous locations overriding existing garbage and rest of the garbage will be cleaned.

Ref: http://www.brpreiss.com/books/opus5/html/page414.html#SECTION0014000000000000000000

- kg January 15, 2007 | Flag Reply


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