how JScript cleans up铪铪铪

本文介绍了JScript中使用的标记清除垃圾回收机制的工作原理及其触发条件。此外,还提供了一个使用ActiveX将表单输入写入电子表格单元格的示例,并讨论了如何避免垃圾回收可能导致的对象生命周期不确定性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

From http://msdn.microsoft.com/msdnmag/issues/01/05/web/

JScript uses a mark-and-sweep garbage collector with a variety of heuristics used to determine when to run garbage collection. The JScript garbage collector works like this:

  1. When the script engine is shut down, garbage is collected.
  2. When 256 variants, or more than 64KB of strings, or more than 4096 array slots have been allocated, the garbage collector sets a flag that says collect soon.
  3. Whenever a new statement is executed or the script debugger starts, that flag is checked, and if it is set, a collection is done.

There is an undocumented JScript function called CollectGarbage that forces a garbage collection. This is for testing purposes only—do not ship code that calls this function. It is a poor programming practice to write code in JScript that depends on garbage collections being done at particular times. If you need predictable garbage collection, use a language that supports it (like Visual Basic® or VBScript). Note that all of this is the implementation detail of the engine and should not be relied upon because it may change in the future. Note also that the version of JScript supported by Microsoft® .NET will use the .NET Framework garbage collector, a multigenerational mark-and-sweep collector.
And remember, if you want a deterministic-lifetime app, use a deterministic-lifetime language like C++, Visual Basic 6.0, or VBScript; not an indeterministic-lifetime language like JScript, Scheme, or Java. If you're writing a program that depends on being able to have a deterministic object lifetime, JScript is not the right tool for the job. Trying to make it a deterministic-lifetime language will just create headaches down the road.

From http://www.faqts.com/knowledge_base/view.phtml/aid/10251

This function uses ActiveX to get a forms input into a spreadsheet cell.function xlWrite(r,c) {  var xlApp, xlSheet;  xlApp = new ActiveXObject("Excel.Application");  xlApp.Visible = true;  xlApp.Workbooks.Add();  xlSheet = xlApp.ActiveSheet;  xlSheet.Cells(r,c).Value = frm.txt.value;  xlSheet.SaveAs("C://xlText.xls");Note that Excel has a bug and doesn't shut down after calling the Quit() method when automating from JScript. To work around this problem, the CollectGarbage() method is used to force JScript's garbage collection to occur almost immediately.  xlApp.Quit();  xlApp = null;  setTimeout("CollectGarbage()",1);}
Beware of garbage collector. If your declare your DHTML objects inside a function, it may be garbage collected when it goes out of scope and disappears.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值