Previously I have done some posts about Shenandoah, and now it is time to talk about Oracle’s ZGC.

ZGC has an algorithm very similar to Shenandoah only it uses coloured pointers to mark the objects, and that demands 64bits, so it doesn’t have 32bits of ZGC, necessary for the reference colouring. As Shenandoah it aims for very small pauses, i.e. sub-millisecond, handle of high heap and scalable pauses. It is available for JDK 11 on Windows, Linux 64, macOS and Linux/Aarch architectures.

Like G1GC, and much of contrary to CMS, it does not have that many flags to set as well. As Shenandoah you can play/tune it a bit, with basic initial heap size, number of concurrent GC threads, NUMA, Huge and large pages. On some versions one can set AlwaysPreTouch.

Arquillian testing


Arquillian testing framework helps considerably doing a simple Wildfly/EAP test.

Reasons to be very easy:
1. It is pretty simple to write a test, as any unit test should be and it allows to package the application in one function pretty smothly:

    public static WebArchive createDeployment()
        System.out.println("create Deployment");

        WebArchive archive = ShrinkWrap.create(WebArchive.class, "sample.war")
                .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
                .addAsResource("validation.xml", "META-INF/validation.xml");

        System.out.println(archive.toString(true)); File("/tmp/" + archive.getName()), true);

        return archive;

2. Using arquilllian.xml enables to select a certain specific container to be deployed, as little as the lines below to select the container:

  <container qualifier="arquillian-wildfly-managed">
      <property name="chameleonTarget">${}</property>
      <property name="serverConfig">standalone-full.xml</property>

3. Testing is pretty straightforward

    public void Test1() throws Exception
        System.out.println("Calling URI: " + serviceUri.toString()); //to show the url being called

        final String targetUrl = serviceUri.toString() + "hello";"The target URL is: {}", targetUrl);
        ResteasyClient client = new ResteasyClientBuilder().build();
        ResteasyWebTarget target =;
        ValidatedJaxRsInterface validatedJaxRs = target.proxy(ValidateJaxRsInt.class);
  "Validated call responded with: {}", validation.Hello());
        catch (Exception exception)
            log.error("Got an error when calling the validated endpoint: {}", exception.getMessage(),exception);
            if (exception instanceof BadRequestException)
                BadRequestException exp = (BadRequestException) exception;
                log.error("The response was bad request :/ {}", exp.getMessage());
        }"Calling validation.Hello");

4. One can use chameleon or wildfly arquillian straight on the pom to get the container, like below:

        <>jboss eap:${jboss.version}:${container.type}</>

Depending on the EAP version one should use the wildfly arquillian container image instead, see difference below:

<!-- Arquillian Chameleon -->

<!--- wildfly-arquillian-container-managed -->

5. Adding validation becomes almost trivial with the validation.xml file:

   <executable-validation enabled="true">

taglib in jsp


On JSP, yes JSP, the taglib directive declares that your JSP page uses a set of custom tags, the usage of JSTL can be quick straightforward:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<%@ taglib uri="" prefix="c" %>  

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello from JSP</title>
${label} <br>

<c:out value="Example taglib" /> <!-- the c is the tag lib using the core jstl core taglib -->
	<c:out value="Let's output this" />
	<c:set var="dummyName" scope="session" value="2000"/>
	<c:out value = "${dummyName}" />
	<c:if test="${dummyName!=null}">
		No estoy embarasada!
	<a href = "<c:url value = ""/>">URL test</a>
	<c:forEach var="headerValue" items="${header}">
		${headerValue.key},${headerValue.value}<BR />


For a complete tutorial, my favourite references are TutorialsPoint and W3 Processing, for sure. Seeing what is useful for you project. It is interesting that one Eclipse/Netbeans just click Dynamic Web Project striaght way, no need for web.xml file as before.

JSP is appropriate for fast prototyping and easily deployment. Of course this technology is being deprecated for Django/Python, which I already had made some posts here, but will do more eventually.

StringBuilder, StringBuffer and JVM flags for Strings


When dealing with Strings, which are of course immutable objects in Java, it may be easier to use a StringBuilder or StringBuffer implementation – as suggested by Geekforgeeks

StringBuffer vs StringBuilder

StringBuffer has a slight better performance than StringBuilder and is able to deal with multiple thread accessing. But is good to know it is there.

 StringBuilder stringBuilderExample = new StringBuilder("Example");
 stringBuilderExample.append("of String Builder");

 StringBuffer stringBuffer = new StringBuffer("Example");
 stringBuffer.append(" of String Buffer");

But factually, comparing both of them in a small benchmark, the performance is the same pretty much, but for a small benchmark, stringBuffer uses a little bit less memory.

real	0m0.048s <----------------------------- 0.048s (from 0.035s up to 0.055s)
user	0m0.044s
sys	0m0.009s

String JVM flags

There are some JVM flags that come in hand when dealing with strings as well:

JVM flagResult

Special thanks

Special thanks to Francesco Marchioni and his amazing blog, the very famous mastertheboss blog. I’m his fan and pretty much have his whole collection of books on my table, DataGrid, Quarkus, EAP, JBoss.

Quick start mbean


Client Mbean

Using a JMX client one can connect to a container Server MBeans and get information very easily. On this blog I have wrote about Jconsole and JvisualVM.

My colleague, Alexander Barbosa, wrote this nice Tutorial to get info on Heap using Mbean. Basically the information comes from the `java.lang:type=Memory`. To get heap or non-heap memory.

	ObjectName memoryMXBean = new ObjectName("java.lang:type=Memory");

You may need to add the credentials for connection and using a map this can be done quick straightforward:

		credentials[0] = "admin";
		credentials[1] = "admin";
		map.put(JMXConnector.CREDENTIALS, credentials);

		// passing server credentials
        JMXConnector jmxConnector = JMXConnectorFactory.connect(serviceURL, map);

Silly mistakes to avoid:

Congratulations you forgot the `.java` part – there is nothing my sully than that:

$ javac -classpath .:$$JBOSS_HOME/bin/client/jboss-client.jar jmxTesterror: 
Class names, 'jmxTest', are only accepted if annotation processing is explicitly requested
1 error



Xms specifies the initial memory allocation. Xmx sets the maximum memory allocation.

For a regional collector, like G1GC or Shenandoah, the pre-allocate the memory because they divide the heap in sections.

On the other hand, the ParallelGC, which is a generational collector – will divide the heap in generations: young, survivor, tenure generation.

But interestingly, ParallelGC will not pre-allocate the memory and as consequence one could cheat the memory and set a Xmx and Xms higher than the memory available in the box, on the example below it has only 2Gb available:

#grep MemTotal /proc/meminfo
MemTotal:        2000000 kB <-------- 2Gb
$JAVA_HOME/bin/java -Xmx20G -Xms2520m -Dfoo=example -XX:+PrintFlagsFinal GetValue <--- just prints string: Running Example
Running example

In contrast to G1/Shenandoah, which will pre-allocate/allocated at the start directly and it will crash right away:

$JAVA_HOME/bin/java -XX:+ShenandoahLogDebug -Xmx20G -Xms1700m -XX:+UseG1GC -Dfoo=example GetValue
OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x00000002c0000000, 1782579200, 0) failed; error='Cannot allocate memory' (errno=12)
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 1782579200 bytes for committing reserved memory.
# An error report file with more information is saved as:
# /root/test_openjdk8_shenandoah_bug/test_application/hs_err_pid28074.log <------------------------------------------------------ crash log with insufficient memory
$JAVA_HOME/bin/java -XX:+ShenandoahLogDebug -Xmx20G -Xms1600m -XX:+UseG1GC -Dfoo=example GetValue
Running Application <------------------------------------------------------------------------------------------------------------ using Xms as 1.6Gb maximum

But of course, eventually setting a higher xms/xmx than the max memory available on the box it will crash, even if it is not pre-allocated/allocated in the start of the JVM.



The usage of this JVM flag is for as memory checks or checks on JNI functions.

This flag sometimes, depending on the function, it shows many warnings.

Check:jni, helps to debug applications that use the Java Native Interface (JNI).

java -Xcheck:jni -verbose:jni TestJNI
 execve("/jdk-11.0.1/bin/java", ["/jdk-11."…, "-Xcheck:jni", "-verbose:jni", "TestJNI"], 0x7ffd7b68e388 /* 72 vars */) = 0
 brk(NULL)                               = 0xb48000
 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fda7bdff000
 readlink("/proc/self/exe", "/jdk-11."…, 4096) = 44
 access("/etc/", R_OK)      = -1 ENOENT (No such file or directory)
 open("/jdk-11.0.1/bin/../lib/jli/tls/x86_64/", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
 stat("/jdk-11.0.1/bin/../lib/jli/tls/x86_64", 0x7fff832f29b0) = -1 ENOENT (No such file or directory)
 open("/jdk-11.0.1/bin/../lib/jli/tls/", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
 stat("/jdk-11.0.1/bin/../lib/jli/tls", 0x7fff832f29b0) = -1 ENOENT (No such file or directory)
 open("/jdk-11.0.1/bin/../lib/jli/x86_64/", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
 stat("/jdk-11.0.1/bin/../lib/jli/x86_64", 0x7fff832f29b0) = -1 ENOENT (No such file or directory)
 open("/jdk-11.0.1/bin/../lib/jli/", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
 stat("/jdk-11.0.1/bin/../lib/jli", {st_mode=S_IFDIR|0775, st_size=1024, …}) = 0
 open("/jdk-11.0.1/bin/../lib/tls/x86_64/", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)

And of course the check part:

mmap(NULL, 1052672, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK, -1, 0) = 0x7fda7bcdc000
 clone(child_stack=0x7fda7bddbfb0, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0x7fda7bddc9d0, tls=0x7fda7bddc700, child_tidptr=0x7fda7bddc9d0) = 3660
 futex(0x7fda7bddc9d0, FUTEX_WAIT, 3660, NULLChecked JNI functions are being used to validate JNI usage
 [Dynamic-linking native method java.lang.Object.registerNatives … JNI]
 [Registering JNI native method java.lang.Object.hashCode]
 [Registering JNI native method java.lang.Object.wait]
 [Registering JNI native method java.lang.Object.notify]
 [Registering JNI native method java.lang.Object.notifyAll]
 [Registering JNI native method java.lang.Object.clone]
 [Dynamic-linking native method java.lang.System.registerNatives … JNI]
 [Registering JNI native method java.lang.System.currentTimeMillis]
 [Registering JNI native method java.lang.System.nanoTime]
 [Registering JNI native method java.lang.System.arraycopy]
 [Dynamic-linking native method java.lang.Class.registerNatives … JNI]
 [Registering JNI native method java.lang.Class.getName0]



Using a fast approach to see the heap

Sometimes taking a heap dump will be too long for the production environment, on this cases, it is possible to wait for the load to reduce and then take the heap dump.

However, it is possible to use the quick java diagnostics tool to see the some quick information.

If one does `GC.class_histogram`, which does not depend of +UnlockDiagnosticVMOptions, it can see the list of instances and retention based on the heap usage, example:

GC.class_stats$ jdk-11.0.1/bin/jcmd 1568 GC.class_histogram
  num     #instances         #bytes  class name
 1:       2725548       87217536  java.util.HashMap$Node
    2:        101237       56724056  [B
    3:       2706313       43301008  org.infinispan.server.some.Class <--- some clas takes 433k bytes, so then 43mb
    4:         13443       17843296  [Ljava.util.HashMap$Node;
    5:         21725       16866280  [Ljava.lang.Object;
    6:          8658        5679648
    7:         62530        5015664  [C
    8:         79376        2540032  java.util.concurrent.ConcurrentHashMap$Node

So we can see that the class `org.infinispan.server.some.Class` takes 2706313bytes, so 27mb of the heap, very easily.

This is a very powerful and pretty simple, you can use jcmd to get a heap dump with: `jcmd PID GC.heap_dump` but then you need to set a tool to analyse the Heap itself, like MAT.

Of course this quick usage is not for beginners, you need to know a bit of your application/stack so then one can see how it gets some data. But for a quick investigation it is pretty useful.

I will be presenting some quick jcmd usage in a conference: TDC. This is my second time presenting at the TDC and I’m very glad to share my knowledge on jcmd and live gc observations. In 2014 went to Florianopolis to present about EEG but now I will be presenting in Porto Alegre, RS, Brazil:

TDC 2020 Porto Alegre

I will add slides/presentation here and some extra comments right after, of course.