0

I searched for this question and got many many matches on stackoverflow itself, but the answers in there are sort of contradicting.

In Ques: How to add VM options to jar? the top-voted+accepted says it is not possible and also most of the answers in Ques: Can I set Java max heap size for running from a jar file? say that No, it is not possible. Most of these answers saying "not possible" were given by people with high reputation and therefore I assume they cannot all be just wrong by coincidence.

One guy said that it can be done by using this others said to make a installer for it, or use Launch4J or make batch files or make another JAR and run the main through this but most of these did not get many votes as compared to those saying "no".

So is it really possible or not? My problem is that I run out of heap space and therefore I want to increase it in JAR.

Q1. I have set increased Heap space from netbeans, will it be increased in the JAR too? (I think no, I am just confirming this one)

Q2. What should I do now to make the increased heap space in JAR? (I am looking for an easy to do way because I do not have knowledge about batch scripting and all and also that I am already putting an Installer to place these files(Advanced Installer) so I do not want to put additional Installers to do this) Is there a simple way out?

Community
  • 1
  • 1
Daksh Shah
  • 2,997
  • 6
  • 37
  • 71

2 Answers2

1

A1. When running your application from Netbeans you spawn a JVM process which executes the application. Setting the heap size from Netbeans simply means it will launch the JVM with the max heap size you configured. It will not effect the jar you are creating in any way.

A2. You cannot configure the heap size inside your jar. This cannot be done either programmatically or by some Manifest configuration.
Setting the max heap can only be done by passing the right JVM options when launching the JVM and this can be done by either having some kind of a startup script or by using a Java launcher as mentioned in the many answers on stackoverflow.
The option of a startup script is better in my opinion as it allows the end user to control the memory setting if needed. With a launcher the memory settings are usually hardcoded and cannot be changed. I suggest you should take a look at the many startup scripts that are available for various open source Java products. Another option would be searching for "java startup script" or similar.

Dror Bereznitsky
  • 20,048
  • 3
  • 48
  • 57
1

Q1. I have set increased Heap space from netbeans, will it be increased in the JAR too?

The memroy use in JVM can be explain blow:



 
  init 
  represents the initial amount of memory (in bytes) that
      the Java virtual machine requests from the operating system
      for memory management during startup.  The Java virtual machine
      may request additional memory from the operating system and
      may also release memory to the system over time.
      The value of init may be undefined.
 
 
 
  used 
  represents the amount of memory currently used (in bytes).
 
 
 
  committed 
  represents the amount of memory (in bytes) that is
      guaranteed to be available for use by the Java virtual machine.
      The amount of committed memory may change over time (increase
      or decrease).  The Java virtual machine may release memory to
      the system and committed could be less than init.
      committed will always be greater than
      or equal to used.
 
 
 
  max 
  represents the maximum amount of memory (in bytes)
      that can be used for memory management. Its value may be undefined.
      The maximum amount of memory may change over time if defined.
      The amount of used and committed memory will always be less than
      or equal to max if max is defined.
      A memory allocation may fail if it attempts to increase the
      used memory such that used > committed even
      if used <= max would still be true (for example,
      when the system is low on virtual memory).
 
 
 

Q2. What should I do now to make the increased heap space in JAR?

May be you can do this by some tricks, like wrap you jar in another jar.

Copy you jar in another_project/resources/ and package another_project to a jar.

The code blow is just a sample, maybe can not run or compile.

public static void main(String[] args) {
    String jarFile = this.getClass().getResource("/resources/you.jar").getFile();
    Runtime.getRuntime().exec("java -Xmx256m -jar "+jarFile);
}
lichengwu
  • 4,277
  • 6
  • 29
  • 42