Adam Lee
Adam Lee

Reputation: 25768

Can Javascript do some explicit/manual memory operation?

In C, if I don't need some specific memory region, I could delete such memory region manually. I am wondering in Javascript, I can do the same thing.

Or Is there some virtual memory interface I can leverage.

Upvotes: 1

Views: 50

Answers (2)

Paul Draper
Paul Draper

Reputation: 83323

No. Nor should you "need" to. Javascript manages the memory, and does not expose its implementation.

(This becomes very, very useful for things like garbage collection, particularly the stop-and-copy variety.)

As soon as you have no references to an object, Javascript can deallocate that memory. Implementations vary in how often they actually deallocate the memory (aka garbage collect), but if you ever run out of memory, they will certainly do it then.

var a = {a: 1};
// can't garbage collect {a: 1}
a = null;
// can garbage collect {a: 1}

var b = {b: 1};
// can't garbage collect {b: 1}
var c = b;
// can't garbage collect {b: 1}
b = null;
// can't garbage collect {b: 1}
c = null;
// can garbage collect {b: 1}

Perhaps you should ask a higher-level question, about what you are trying to accomplish.

Upvotes: 1

Javascript does not allow you to manually clear the memory. And you should not be concerned about it either when it does it for you.

On the other hand javascript clears used memory when there are no references to that memory. All you can do is to cut those references. Let's say:

var x = some object;

x = null;

Upvotes: 1

Related Questions