简体   繁体   English

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

[英]How do I allow the user to easily choose how much memory to allocate in a Java Swing app?

We have a Swing app that processes relatively large amounts of data. 我们有一个处理相对大量数据的Swing应用程序。 For instance we currently process CSV files with millions of rows of data. 例如,我们目前处理具有数百万行数据的CSV文件。 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. 但是,不同的用户将需要处理不同数量的数据以及不同数量的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? 是否有任何简单的方法允许用户指定堆而无需手动编辑配置或.bat文件? 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. 然后,用户可以选择最适合他们的那个。

I recommend having something similar to IntelliJ. 我建议使用与IntelliJ类似的东西。 It brings up a configuration dialog when an OutOfMemoryException occurs. 当发生OutOfMemoryException时,它会弹出一个配置对话框。 This dialog allows the user to configure the heap size and stores it to idea.exe.vmoptions . 此对话框允许用户配置堆大小并将其存储到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. 您需要将文件的内容转储到java / javaw启动命令中,或者使用一个Java程序引导程序并启动实际程序。

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! 支持此解决方案非常重要,您希望并以避免数据损坏的方式处理OutOfMemoryExceptions!

I would write a short startup.jar with very few fixed memory settings starting from a script. 我会写一个简短的startup.jar,从脚本开始只有很少的固定内存设置。 In turn startup.jar will start your target app with Runtime.exec() and adjusted parameters. 反过来startup.jar将使用Runtime.exec()和调整后的参数启动目标应用程序。

You end up in 2 JVM instances that spend more memory than only one JVM. 您最终会在2个JVM实例中占用更多内存,而不仅仅是一个JVM。 But if your application will spend a lot of memory anyway the first JVM does not matter. 但是如果你的应用程序无论如何都要花费大量内存,那么第一个JVM并不重要。

The main idea would be this: 主要想法是这样的:

  • Launch your main application through a shell script similar to this: 通过类似于此的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
  • In your Swing application, have a menu option Memory settings... . 在Swing应用程序中,有一个菜单选项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 . 当用户单击“ 应用”或“ 确定”时 ,根据用户选择的settigns(在您的情况下为-Xmx )使用命令行选项覆盖用户配置文件,并显示类似重新启动应用程序以应用这些设置的消息

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: 捕获outofmemory excpetion,lauch用户对话框来设置内存限制,将此值传递给使用Runtime.exec的线程()使用java -cp lib的jar vmoptions重新启动应用程序将启动指定内存大小的新jvm user.sample:

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

You did not mention how you are creating the installer. 您没有提到如何创建安装程序。 If you do not already use it, I suggest using NSIS . 如果你还没有使用它,我建议使用NSIS The documentation is good and it has a plugin for eclipse. 文档很好,它有一个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. 您可以使用该向导生成nsi文件,然后手动编辑该文件,以获取向导不可用的选项。

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. 我不确定它是否可行,但我认为一旦安装了程序,用户就可以重新运行安装程序并选择其他选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM