Skip to main content

Object Lifetime

  • Chapter
  • First Online:
Pro .NET Memory Management
  • 2532 Accesses

Abstract

Previous chapters describe the automatic memory management process in .NET quite comprehensively. Chapter 6 contains information about how objects are created, while chapters from 7 to 11 inform in detail how they are collected when no longer needed. However, there are some side mechanisms, without the description of which our knowledge would not be complete. In this chapter we will focus on three such mechanisms. Although they exist separately and can be used independently, they relate to each other conceptually. All of them concern a common topic - the lifetime of the object.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 79.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 99.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Notes

  1. 1.

    In the case of Linux resources, they are commonly represented as regular integers.

  2. 2.

    Even worse, due to resurrection and possible timing, there may be multiple simultaneous calls to the same finalizer.

  3. 3.

    Refer to the early root collection technique described in Chapter 8.

  4. 4.

    Literature calls it “finalizer reachable,” but the mentioned name better aligns with the .NET naming.

  5. 5.

    For simplicity it happens on each iteration although we could call it, for example, periodically. Such a little contrived example allows us to better illustrate some diagnostic problems.

  6. 6.

    There is an issue https://github.com/Microsoft/perfview/issues/722 created to fix that, so you can track it whether it has been already fixed at the time of your reading.

  7. 7.

    As explained earlier, suppressing finalization logic is trivial, based only on setting a single bit in an object header. Thus, we should not be afraid of its overhead (for example, by calling it twice on the same object both from the derived as well as from the base class).

  8. 8.

    They are introduced to provide a standardized way of consuming handles as most often indeed those values are treated as invalid handles.

  9. 9.

    We are on thin ice here, depending on deep implementation details how objects get promoted. For example, with the current implementation, if the target object is pinned (or becomes a part of extended pinned plug), it may be demoted and we will be calling our callback again for ephemeral GCs also.

Author information

Authors and Affiliations

Authors

Rights and permissions

Reprints and permissions

Copyright information

© 2018 Konrad Kokosa

About this chapter

Check for updates. Verify currency and authenticity via CrossMark

Cite this chapter

Kokosa, K. (2018). Object Lifetime. In: Pro .NET Memory Management. Apress, Berkeley, CA. https://doi.org/10.1007/978-1-4842-4027-4_12

Download citation

Publish with us

Policies and ethics