-5

I have a variable oneDay for which I have assigned an integer number

 var oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds

I'm releasing the memory occupied by oneDay using the below syntax at the end of the function termination in which the code has been declared.

 var oneDay=null;

The error that I'm getting :

error TS2134: Subsequent variable declarations must have the same type. Variable 'oneDay ' must be of type 'Date', but here has type 'null'.

What could be the possible solution for this??Thanks

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
forgottofly
  • 2,729
  • 11
  • 51
  • 93

2 Answers2

7

Memory is managed for you in JavaScript.

All modern browsers use a mark-and-sweep algorithm to detect unreachable objects (some older browsers use a reference-counting algorithm, which fails to collect objects where there is a reference loop as there will always be a reference) *.

As soon as an object can no longer be referenced it is eligible for garbage collection (although garbage collection will happen "at some point", not immediately).

On the whole, you don't need to concern yourself with memory management in JavaScript or TypeScript - unless you have a measurable problem.

(* From Pro TypeScript, p168-170)

Fenton
  • 241,084
  • 71
  • 387
  • 401
1

Cause: You are re declaring this variable that why you are getting this error. Try this:

oneDay = null;
$scope.days = null

or

delete $scope.days 
Jain
  • 1,209
  • 10
  • 16
  • 2
    `delete oneDay` will do absoulutely nothing. –  Dec 23 '14 at 05:56
  • This accepted answer unfortunately is off the mark. Setting the variable to be null is absolutely useless (since storing `null` takes up the same amount of space as storing the number of milliseconds in a day), even if you needed to worry about memory management, which of course you don't. –  Dec 25 '14 at 05:58
  • My answer is okay after update null can deallocate the memory space of varible. Please read it: http://stackoverflow.com/questions/2999604/best-way-to-deallocate-an-array-of-array-in-javascript – Jain Dec 25 '14 at 06:25
  • In that case, he is "deallocating" a complex object (an array). You are trying to "deallocate" a scalar (single value, primitive) by setting it a different scalar (null). Setting a scalar to null does not release anything, it just changes its value to null. There may be some extremely special cases where you need to help JS out with its GC by setting complex objects to null, but the real answer to this question, repeat very carefully after me, is **you do not need to do any allocation, or deallocation, or any other form of memory management in JavaScript**. –  Dec 25 '14 at 06:55
  • 1
    Also, read the answer from @SteveFenton, who ought to know what he's talking about with a rep of 60K. His answer should be marked as the accepted one. –  Dec 25 '14 at 06:57