How is memory deallocated?

How is memory deallocated?

The new operator allocates memory for objects.How isthis memory deallocated?

If you have ever programmed in C++, you might wonder why JavaScript doesnot have a delete operator to match the newoperator. The reason for this is simple: It’s not necessary!

In JavaScript, memory for objects is automatically reclaimed wheneverthere is no longer need for the object. The JavaScript interpretertracks references to objects and deletes them when variables no longerrefer to them.

For example, consider the following JavaScript code:

d = new Array (50);d = null;

The first statement allocates memory for an Array objectlarge enough to hold 50 elements, and then records the fact that the newobject is referenced by a variable named d.

The next statement, however, changes d so that instead ofreferring to the Array object, it contains thespecial value null. Since there is no longer any way toaccess the Array object, the JavaScript interpreterdestroys it by reclaiming the memory that was allocated to it.

Reclaiming memory allocated to objects that can no longer be accessedis called garbage collection. Allocated objects nolonger accessible by variables are called orphan objects.

This next example shows how to modify the above code to prevent thedestruction of the Array object:

d = new Array (50);
e = d; //
makee refer to the same object asd
d = null; //
disassociate d from the Arrayobject

Since the Array object is referenced by e at themoment it is abandoned by d, the Array objectremains intact.

In conclusion, there is no need to worry about destroying objects inJavaScript. Garbage collection is performed automatically by theinterpreter whenever objects are no longer useful or documents areunloaded. If you want to explicitly destroy an object, you can do so byabandoning it ? that is, by making sure there are no variables thatrefer to it. (Setting the variables to null works prettywell for this.)

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing