ArticleslgStudy

computer science

Object resurrection

Object resurrection is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Object resurrection rather than just read about it. In short: In object-oriented programming languages with garbage collection, object resurrection occurs when an object becomes reachable (in other words, no longer garbage) during the process of object destruction, as a side effect of a finalizer being executed. Object resurrection causes a number of problems, particularly that the possibility of object resurrection – even if it does not occur – makes garbage collection signif…

Key takeaways

  • Object resurrection belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Object resurrection to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Object resurrection from memory before moving on to harder problems.

Reference excerpt

In object-oriented programming languages with garbage collection, object resurrection occurs when an object becomes reachable (in other words, no longer garbage) during the process of object destruction, as a side effect of a finalizer being executed. Object resurrection causes a number of problems, particularly that the possibility of object resurrection – even if it does not occur – makes garbage collection significantly more complicated and slower, and is a major reason that finalizers are discouraged. Languages deal with object resurrection in various ways. In rare circumstances, object resurrection is used to implement certain design patterns, notably an object pool, while in other circumstances resurrection is an undesired bug caused by an error in finalizers, and in general resurrection is discouraged.

Process Object resurrection occurs via the following process. First, an object becomes garbage when it is no longer reachable from the program, and may be collected (destroyed and deallocated). Then, during object destruction, before the garbage collector deallocates the object, a finalizer method may be run, which may in turn make that object or another garbage object (reachable from the object with a finalizer) reachable again by creating references to it, as a finalizer may contain arbitrary code. If this happens, the referenced object – which is not necessarily the finalized object – is no longer garbage, and cannot be deallocated, as otherwise the references to it would become dangling references and cause errors when used, generally program crash or unpredictable behavior. Instead, in order to maintain memory safety, the object is returned to life or resurrected. In order to detect this, a garbage collector will generally do two-phase collection in the presence of finalizers: first finalize any garbage that has a finalizer, and then re-check all garbage (or all garbage reachable from the objects with finalizers), in case the finalizers have resurrected some garbage. This adds overhead and delays memory reclamation.

Resurrected objects A resurrected object may be treated the same as other objects, or may be treated specially. In many languages, notably C#, Java, and Python (from Python 3.4), objects are only finalized once, to avoid the possibility of an object being repeatedly resurrected or even being indestructible; in C# objects with finalizers by default are only finalized once, but can be re-registered for finalization. In other cases resurrected objects are considered errors, notably in Objective-C; or treated identically to other objects, notably in Python prior to Python 3.4. A resurrected object is sometimes called a zombie object or zombie, but this term is used for various object states related to object destruction, with usage depending on language and author. A "zombie object" has a specialized meaning in Objective-C, however, which is detailed below. Zombie objects are somewhat analogous to zombie processes, in that they have undergone a termination state change and are close to deallocation, but the details are significantly different.

Variants In the .NET Framework, notably C# and VB.NET, "object resurrection" instead refers to the state of an object during finalization: the object is brought back to life (from being inaccessible), the finalizer is run, and then returned to being inaccessible (and no longer is registered for future finalization). In .NET, which objects need finalization is not tracked object-by-object, but instead is stored in a finalization "queue", so rather than a notion of resurrected objects in the sense of this article, one speaks of objects "queued for finalization". Further, objects can be re-enqueued for finalization via GC.ReRegisterForFinalize, taking care to not multiply enqueue objects.

Mechanism There are two main ways that an object can resurrect itself or another object: by creating a reference to itself in an object that it can reach (garbage is not reachable, but garbage can reference non-garbage objects), or by creating a reference in the environment (global variables, or in some cases static variables or variables in a closure). Python examples of both follow, for an object resurrecting itself. It is also possible for an object to resurrect other objects if both are being collected in a given garbage collection cycle, by the same mechanisms. Resurrects itself by creating a reference in an object it can reach:

Resurrects itself by creating a reference in the global environment:

In the above examples, in CPython prior to 3.4, these will run finalizers repeatedly, and the objects will not be garbage-collected, while in CPython 3.4 and later, the finalizers will only be called once, and the objects will be garbage-collected the second time they become unreachable.

Problems Object resurrection causes a large number of problems.

Complicates garbage collection The possibility of object resurrection means that the garbage collector must check for resurrected objects after finalization – even if it does not actually occur – which complicates and slows down garbage collection. Indestructible objects In some circumstances an object may be indestructible: if an object is resurrected in its own finalizer (or a group of objects resurrect each other as a result of their finalizers), and the finalizer is always called when destroying the object, then the object cannot be destroyed and its memory cannot be reclaimed. Accidental resurrection and leaks Thirdly, object resurrection may be unintentional, and the resulting object may be semantic garbage, hence never actually collected, causing a logical memory leak. Inconsistent state and reinitialization A resurrected object may be in an inconsistent state, or violate class invariants, due to the finalizer having been executed and causing an irregular state. Thus resurrected objects generally need to be manually reinitialized. Unique finalization or re-finalization In some languages (such as Java and Python 3.4+) finalization is guaranteed to happen exactly once per object, so resurrected objects will not have their finalizers called; therefore, resurrected objects must execute any necessary cleanup code outside of the finalizer. In some other languages, the programmer can force finalization to be done repeatedly; notably, C# has GC.ReRegisterForFinalize.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Object resurrection

Start with the simplest possible case. Write down what Object resurrection claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Object resurrection before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Object resurrection ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Object resurrection

In research
Object resurrection appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Object resurrection in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Object resurrection is common in secondary-school and first-year university syllabi. It links to neighbouring topics Memory management, Object (computer science), so understanding it makes those chapters shorter.
In everyday life
Look for Object resurrection outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Object resurrection” →

How to study Object resurrection in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Object resurrection means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Object resurrection out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Object resurrection in simple terms?

In object-oriented programming languages with garbage collection, object resurrection occurs when an object becomes reachable (in other words, no longer garbage) during the process of object destruction, as a side effect of a finalizer being executed. Object resurrection causes a number of problems…

Why does Object resurrection matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Object resurrection?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Object resurrection.

Tags

  • Memory management
  • Object (computer science)

Keep exploring