0

I have below line -

lax.nl.java.option.additional=-Xms1000m -Xmx2048m -Xss2m -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:MaxPermSize=128m -Dsun.java2d.noddraw=true -Djava.library.path=native -Djava.security.auth.login.config==login_configuration.config

And i want to extract the Xmx value i.e. 2048 in this case (It can also be 512 some times). Not sure how i should do this using java substring or any other way , Please help! . Thanks

Nabin
  • 11,216
  • 8
  • 63
  • 98
  • thatLine.split("Xmx")[1].split("m")[0] – Nabin May 08 '17 at 04:51
  • 1
    Are you sure you don't want to match the `m` as well? What if somebody specifies `-Xmx2G`? – Henry May 08 '17 at 04:56
  • The context is **the line** @Henry – Nabin May 08 '17 at 04:59
  • Possible duplicate of [How to get vm arguments from inside of java application?](http://stackoverflow.com/questions/1490869/how-to-get-vm-arguments-from-inside-of-java-application) – Ryan Leach May 08 '17 at 04:59
  • @Spiderman my comment was directed to the OP. – Henry May 08 '17 at 05:00
  • @RyanTheLeach Don't see how this is a dup? – Michael Markidis May 08 '17 at 05:00
  • Do you want to match that out of the string? Or do you want the maximum memory from the currently running VM? Additionally, the ending of that number can vary. -Xms The -Xms option sets the initial and minimum Java heap size. The Java heap (the “heap”) is the part of the memory where blocks of memory are allocated to objects and freed during garbage collection. Note: -Xms does not limit the total amount of memory that the JVM can use. Operation Format: -Xms[g|G|m|M|k|K] Combine -Xms with a memory value and ad – Ryan Leach May 08 '17 at 05:01

2 Answers2

3

The comment given by @Spiderman might be a quick one line solution. But in general the way to approach this type of regex problem in Java is to use a Pattern with a Matcher. The following regex can be used here:

.*\s-Xmx(\d+)(\w+)\s.*

Full code:

String line = "lax.nl.java.option.additional=-Xms1000m -Xmx2048m -Xss2m -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:MaxPermSize=128m -Dsun.java2d.noddraw=true -Djava.library.path=native -Djava.security.auth.login.config==login_configuration.config";

String pattern = ".*\\s-Xmx(\\d+)(\\w+)\\s.*";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(line);
if (m.find( )) {
    System.out.println("Found value: " + m.group(1));
    System.out.println("Found unit: " +  m.group(2));
}

Output:

Found value: 2048
Found unit: m

Demo here:

Rextester

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Thanks for mentioning that. But beginners will definitely prefer split method over regex. But regex is far more better solution than split. Thanks – Nabin May 08 '17 at 04:58
  • @Spiderman If the use case gets more complex, your splitting method might no longer be feasible. Don't know if you're the downvoter. – Tim Biegeleisen May 08 '17 at 05:00
  • Downvoter is someone who has done -1 to me as well. :) . FYI, I am the upvoter – Nabin May 08 '17 at 05:01
  • this is a jvm arguments string, at minimum an accepted answer should be able to deal with multiple endings for the units. https://docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/jrdocs/refman/optionX.html Format: -Xms[g|G|m|M|k|K] – Ryan Leach May 08 '17 at 05:02
  • @RyanTheLeach Let's wait for feedback from the OP. We don't want to overengineer something which isn't needed here. – Tim Biegeleisen May 08 '17 at 05:03
  • @TimBiegeleisen I'd sooner not have a newbie have a bug lying in wait, as well as being an incorrect answer sitting at the top of google. – Ryan Leach May 08 '17 at 05:04
  • Thanks Tim, reversed my -1. – Ryan Leach May 08 '17 at 05:10
  • This solution is probably good enough, but there are still cases where it fails (this always happens if one does not do the "right thing" according to specification), e.g. `-Dparam=xxx-Xmx2048m` – Henry May 08 '17 at 05:11
  • @Henry No, my solution would actually still run in this case, q.v. [here](http://rextester.com/RXVT92031). – Tim Biegeleisen May 08 '17 at 05:16
  • @TimBiegeleisen it matches the wrong value in a string like this ` -Xmx2048m -D=x-Xmx7m` – Henry May 08 '17 at 05:19
0

Also this can be done without using Regex. Simply use split() method as below:

public class HelloWorld {
    public static void main(String[] args) {
        String line = "lax.nl.java.option.additional=-Xms1000m -Xmx2048m -Xss2m -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:MaxPermSize=128m -Dsun.java2d.noddraw=true -Djava.library.path=native -Djava.security.auth.login.config==login_configuration.config";
        String number = line.split("Xmx")[1].split("m")[0];
        System.out.println(number);
    }
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
Nabin
  • 11,216
  • 8
  • 63
  • 98
  • this is a jvm arguments string, at minimum an accepted answer should be able to deal with multiple endings for the units. https://docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/jrdocs/refman/optionX.html Format: -Xms[g|G|m|M|k|K] – Ryan Leach May 08 '17 at 05:02