如何让用户轻松选择在 Java Swing 应用程序中分配多少内存?

发布于 2024-09-18 13:35:45 字数 278 浏览 13 评论 0原文

我们有一个处理相对大量数据的 Swing 应用程序。例如,我们目前处理包含数百万行数据的 CSV 文件。出于性能和简单性的原因,我们将所有数据保留在内存中。然而,不同的用户需要处理的数据量以及 RAM 量也不同。创建安装程序时,我们当然需要指定堆大小。是否有任何简单的方法可以允许用户指定堆,而无需手动编辑配置或 .bat 文件?我猜并不是所有用户都会对此感到满意。

我见过一个示例,其中应用程序指定了三个不同的快捷方式,每个快捷方式都指定了不同的内存量。这可行,但我想要一个更灵活的选项。然后,用户可以选择最适合他们的一种。

We have a Swing app that processes relatively large amounts of data. For instance we currently process CSV files with millions of rows of data. For the reasons of performance and simplicity we just keep all of the data in memory. However different users will have different amounts of data they need to process as well as different amounts of RAM. When creating the installer, of course we need to specify the heap size. Is there any easy way to allow the user to specify the heap without them needing to hand edit a configuration or .bat file? I'm guessing not all users would be comfortable with this.

I've seen one example where an app specified three different shortcuts each with a different amount of memory specified. This could work but I would like an option that is more flexible. The user could then just choose the one that would work best for them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

少女七分熟 2024-09-25 13:35:45

我建议使用类似于 IntelliJ 的东西。当发生 OutOfMemoryException 时,它会弹出一个配置对话框。该对话框允许用户配置堆大小并将其存储到idea.exe.vmoptions。您需要将该文件的内容子到 java/javaw 启动命令中,或者让一个 Java 程序引导并启动真正的程序。

一个非常酷的变体是根据数据大小建议内存大小。用户确实无法知道您的程序需要多少内存,因此您在选择值时提供的任何指导都将对他们有很大帮助。

为了支持此解决方案,您期望并以避免数据损坏的方式处理 OutOfMemoryExceptions,这一点非常重要!

I recommend having something similar to IntelliJ. It brings up a configuration dialog when an OutOfMemoryException occurs. This dialog allows the user to configure the heap size and stores it to idea.exe.vmoptions. You would need to sub the contents of the file into the java/javaw launch command or have one Java program bootstrap and launch the real one.

A really cool variant on this is to suggest a memory size based on the data size. The user really has no way of knowing how much memory your program needs so any guidance you can provide in selecting the value will help them immensely.

It is extremely important in support of this solution that you expect and handle OutOfMemoryExceptions in a way that avoids data corruption!

陌路黄昏 2024-09-25 13:35:45

我会从脚本开始编写一个简短的startup.jar,其中只有很少的固定内存设置。反过来,startup.jar 将使用 Runtime.exec() 和调整的参数启动您的目标应用程序。

您最终会得到 2 个 JVM 实例,它们消耗的内存比仅 1 个 JVM 消耗的内存还要多。但是,如果您的应用程序无论如何都会花费大量内存,那么第一个 JVM 就无关紧要了。

I would write a short startup.jar with very few fixed memory settings starting from a script. In turn startup.jar will start your target app with Runtime.exec() and adjusted parameters.

You end up in 2 JVM instances that spend more memory than only one JVM. But if your application will spend a lot of memory anyway the first JVM does not matter.

披肩女神 2024-09-25 13:35:45

主要思想是:

  • 通过类似于以下的 shell 脚本启动主应用程序:
@echo off
setlocal
REM This reads the JVM command line options from a user configuration file
for /f %x in (%HOMEDRIVE%%HOMEPATH%\myapp.config) do set JVM_OPTIONS=%x
REM Important: call javaw and not java
javaw -jar myApp.jar %JVM_OPTIONS%
endlocal
  • 在 Swing 应用程序中,有一个菜单选项内存设置...。当用户选择它时,解析用户的配置文件并填充设置对话框,以便用户可以更改任何适当的内容。当用户单击应用确定时,根据用户选择的设置,使用命令行选项覆盖用户配置文件(在您的情况下,-Xmx),并显示一条消息,内容类似于重新启动应用程序以应用这些设置

The main idea would be this:

  • Launch your main application through a shell script similar to this:
@echo off
setlocal
REM This reads the JVM command line options from a user configuration file
for /f %x in (%HOMEDRIVE%%HOMEPATH%\myapp.config) do set JVM_OPTIONS=%x
REM Important: call javaw and not java
javaw -jar myApp.jar %JVM_OPTIONS%
endlocal
  • In your Swing application, have a menu option Memory settings.... When the user selects that, parse the user's configuration file and populate a settings dialog so that the user can change whatever it is appropiate. When the user clicks Apply or Ok, overwrite the user configuration file with command line options according to the user's selected settigns (in your case, -Xmx), and show a message saying something like Restart the application to apply these settings.
指尖凝香 2024-09-25 13:35:45

捕获内存不足的异常,启动一个 swing 用户对话框来设置内存限制,将此值传递给使用 Runtime.exec() 的线程,使用 java -cp lib 的 jar vmoptions 重新启动应用程序,这将启动一个新的 jvm,其内存大小指定为用户.样本:

 Runtime.getRuntime().exec("java -cp
 -Xms2560m -Xmx2560m -XX:NewSize=32M -XX:MaxPermSize=256M -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+UseParNewG launch.jar");

catch the outofmemory excpetion , lauch a swing user dialog to set the memory limit, pass this value to a thread that uses Runtime.exec() restart the application using java -cp lib's jar vmoptions that will launch a new jvm with memory size specified to user.sample:

 Runtime.getRuntime().exec("java -cp
 -Xms2560m -Xmx2560m -XX:NewSize=32M -XX:MaxPermSize=256M -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+UseParNewG launch.jar");
⊕婉儿 2024-09-25 13:35:45

您没有提到如何创建安装程序。如果您还没有使用它,我建议使用 NSIS。文档很好,并且有一个 eclipse 插件。您可以使用向导生成 nsi 文件,然后手动编辑该文件以获取向导无法提供的选项。

根据您当前的要求,只需向安装程序添加一个选项页面,然后根据所选选项,将正确的系统属性文件复制到可用于启动程序的用户设置文件夹中。我不确定是否可能,但我认为一旦安装了程序,用户就可以重新运行安装程序并选择不同的选项。

You did not mention how you are creating the installer. If you do not already use it, I suggest using NSIS. The documentation is good and it has a plugin for eclipse. You can use the wizard to generate the nsi file and then edit it manually for the options that are not available with the wizard.

As to your current requirement, simply add a page to the installer for the options and based on the option selected, copy the correct system properties file to the user settings folder that you can use to start your program. I am not sure if its possible but I think once the program is installed, the user can re-run the installer and select a different option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文