Skip to main content

JAVA TIPS

Searching element in array

static boolean exists(int[] ints, int k) {
return Arrays.binarySearch(ints,k) >= 0? true: false;
}

Scanner java

Scanner in = new Scanner(System.in);

String builder inside for loops

StringBuilder builder = new StringBuilder();
builder.append("next");

RAM Usage

When launching a microservice using the mvnw script in the root folder of the JHipster project, two java processes are launched:

  • Java mircroservice spring boot application with embedded Tomcat server
  • Maven wrapper

These two processes can easily consume more than 2GB of RAM by default.

Limit Memory Usage of a Microservice

Edit the mvnw script by adding the following lines at the beginning of the script:

#Edit Maven Wrapper memory
MAVEN_OPTS="-Xms128m -Xmx256m"
#Edit Java mircroservice max memory
MAVEN_OPTS="-Drun.jvmArguments="-Xmx512m" $MAVEN_OPTS"

Note: In general, lowering the maximum allowed memory will increase the CPU usage.

Monitor Memory Usage of Java processes

You can view if a microservice is lacking some memory with the applications:

  • jvisualvm (included in the JDK package, can be found in the bin directory of your JAVA_HOME)
  • VisualVM Download VisualVM
cd /path_to_unzipped_folder
cd bin
./visualvm

This will launch the application and automatically load all your current local java processes (those with JMX enabled).