最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Memory deallocation in TypeScript - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question edited Dec 23, 2014 at 5:38 Felix Kling 817k181 gold badges1.1k silver badges1.2k bronze badges asked Dec 23, 2014 at 4:44 forgottoflyforgottofly 2,72912 gold badges54 silver badges96 bronze badges 17
  • 6 Why do you think that's releasing memory? Just let it go out of scope. – Andrew Barber Commented Dec 23, 2014 at 4:45
  • 5 You don't manually manage memory in JavaScript – Ryan Commented Dec 23, 2014 at 4:45
  • 3 Why are you redeclaring oneday – Jain Commented Dec 23, 2014 at 4:47
  • 1 What @Jain said; that's the actual cause of your error. – Andrew Barber Commented Dec 23, 2014 at 4:47
  • 1 I'm not sure but it works for you $scope.days = null or delete $scope.days – Jain Commented Dec 23, 2014 at 4:53
 |  Show 12 more ments

2 Answers 2

Reset to default 8

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)

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 
发布评论

评论列表(0)

  1. 暂无评论