You are on page 1of 3

Minhaj University Lahore

Submitted by:
Hammad Saeed
Submitted to:
Sir Saif Ali
Roll no:
69
Class:
BSCS (B) 6TH SEMESTER
Subject:
Object oriented analysis and design
Explain Objects and What is the difference between deterministic and non-
deterministic object destruction?

Objects:
An object, in object-oriented programming (OOP), is an abstract data type created by a
developer. It can include multiple properties and methods and may even contain other objects.
In most programming languages, objects are defined as classes.
Objects provide a structured approach to programming. By defining a dataset as a custom
object, a developer can easily create multiple similar objects and modify existing objects within
a program. Additionally, objects provide "encapsulation," meaning the data within an object is
protected from being modified or destroyed by other functions or methods unless explicitly
allowed.
in the C++/CLI specification there is a distinction made between the two. It allows both deterministic
and non-deterministic cleanup, and uses the term "destructor" for the deterministic functionality and
"finalizer" for the non-deterministic functionality.

In languages like C++, objects go out of scope or are deleted and their destructors are executed
immediately. This is known as deterministic destruction. A significant difference with C# is that there is
no way to know when an object’s destructor will be called or if the destructor will ever be called.

In C#, we do not know when the destructor is executed. It is called only during the garbage collection
process and we do not know when it actually happens. This state is called non-deterministic destructors.

Finalization occurs during garbage collection. The finalizer is invoked during finalization to clean up
resources related to the object. Non-deterministic garbage collection is performed on a generation or
Large Object Heap when the related threshold is exceeded. Because of this, there may be some latency
between when an object becomes unreachable and when the Finalize method is called. This may cause
some resource contention. For example, the action of closing a file in the Finalize method may not occur
immediately. This may cause resource contention because, although the file is not being used, it remains
unavailable for a period of time.

Place cleanup code for the non-deterministic garbage collection in the Finalize method, which is
implicitly called in the class destructor. The class destructor cannot be called on demand. As mentioned,
there may be some latency in the Finalize method running. The class destructor is the method of the
same name of class with a tilde (~) prefix.
class XClass {

// destructor

~XClass() {

// cleanup code

For certain types of resources, non-deterministic garbage collection is inappropriate. You should not
release resources that require immediate cleanup. Also, managed objects should not be cleaned up in a
Finalize method. Order of finalization is not guaranteed. Therefore, you cannot assume that any other
managed object has not been already finalized. If that has occurred, referring to that object could raise
an exception.

Non-deterministic garbage collection is neither simple nor inexpensive. The lifetime of objects without a
Finalize method is simpler. For these reasons, the Finalize method should be avoided unless necessary.
Even an empty destructor (which calls the Finalize method), harmless in C++, enlists the object for the
complete non-deterministic ride—a very expensive ride. For the purposes of this chapter, objects with
destructors are called finalizable objects.

You might also like