正确关闭小程序
在我工作的地方,当用户注销我们的应用程序(一个小程序)时,他们使用 AppletContext.showDocument(URL)
方法,提供所需的注销 JSP。但他们也有代码表明,如果由于某种原因他们无法获得 AppletContext 来简单地调用 Applet.destroy() 方法。
我们使用的是瘦客户端架构,这意味着我们本质上有一堆连接到服务器的哑终端。我提到这一点是因为我们通常会运行数十个甚至数百个 JVM 实例 - 每个小程序一个。
在 destroy()
方法中,它们会处理所获取的所有资源,然后获取对 Runtime
的引用并调用 runFinalization()
和 gc()
- 但它不执行 System.exit() 或等效操作。
我理解的问题
- 是,释放资源并让您留在同一网页上,但它对运行小程序的 JVM 有何作用?
- 如果我在
destroy()
末尾添加对System.exit()
的调用,它将对瘦客户端服务器上运行的其他 JVM 产生什么影响?
Where I work they use the AppletContext.showDocument(URL)
method when a user logs off our application, which is an applet, providing the desired logoff JSP. But they also have code that says if for some reason they are unable to get an AppletContext
to simply call the Applet.destroy()
method.
We are using a thin client architecture which means that we essentially have a bunch of dumb terminals connected to a server. I mention this because we will often have dozens if not 100's of instances of JVMs running - one for each applet.
Inside the destroy()
method they dispose of all resources they acquired and then get a reference to Runtime
and call runFinalization()
and gc()
- but it does not do a System.exit()
or equivalent.
Questions
- I understand that frees up resources and leaves you on the same web page but what does it do to the JVM that was running the applet?
- If I add a call to
System.exit()
at the end of thedestroy()
what will it do to the other JVMs that are running on the thin client server?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于上下文来说,这都是必要的,因为当页面处于活动状态时,浏览器(或 Java 插件,或两者)都会挂在您的小程序实例上。即使您从 DOM 中删除它或尝试其他技巧来释放该内存,在导航到不同页面之前,您的小程序实例仍会保留,因此您需要在 destroy() 中完全清理。我已经完成了内存分析,显示它在本机代码中的某个地方被引用。
回答您的问题:
System.exit()
。如果允许的话,在现代浏览器上它会杀死该浏览器的 JVM 实例,而不是其他浏览器。在过去,它可能会关闭整个浏览器:)编辑:
实际上,答案 1 的故事还有更多内容……除了 OS X 之外,其他地方都是如此,在 OS X 中,下一代插件并不是默认插件,直到大约在 6u27 左右(10.6 上是 Java for OS X update 5,10.7 从第一天开始)。
For context, this is all necessary because while a page is active browsers (or the Java plugin, or both) hang on to your applet instance. Even if you remove it from the DOM or try other tricks to release that memory, until you navigate to a different page your applet instance is retained so you need to fully clean up in destroy(). I've done memory profiling which shows it's referenced in native code somewhere.
To answer your questions:
System.exit()
. If it was allowed though, on modern browsers it would kill the instance of the JVM for that browser, none of the others. In the past it would've been likely to shut the entire browser down :)EDIT:
Actually there's more to the story of Answer 1... that's true everywhere except OS X, where the next-gen plugin wasn't the default until somewhere around 6u27 (On 10.6 it was Java for OS X update 5, and 10.7 from day 1).