Java – Java application monitoring tools

jcmd: Prints basic class, thread, and VM information for a Java process
Example)

% jcmd <process_id> VM.uptime
% jcmd <process_id> VM.system_properties
% jcmd <process_id> VM.version
% jcmd <process_id> VM.command_line
% jcmd <process_id> VM.flags [-all]
% jcmd <process_id> Thread.print
% jcmd <process id> GC.run // force to run full GC

jconsole: Provides a graphical view of JVM activities, including thread usage, class usage, and GC activities
jhat: Reads and helps analyze memory heap dumps. This is a postprocessing utility
jmap: Provides heap dumps and other information about JVM memory usage
jinfo: Provides visibility into the system properties of the JVM, and allows some system properties to be set dynamically

% jinfo -flags <process_id>
% jinfo -flag PrintGCDetails <process_id>

jstack: Dumps the stacks of a Java process

jstat: Provides information about GC and class-loading activities

% jstat -compiler <process_id>
% jstat -printcompilation <process_id> <duration(ms)>

jvisualvm: A GUI tool to monitor a JVM, profile a running application, and analyze JVM heap dumps

jmap:

% jmap -clstats : print out information about class loaders(Java 8)
% jmap -permstat : print out information about class loaders(Java 7)
Posted in Java, Programming | Leave a comment

Linux – useful commands to inspect performance utilization

Memory utilization

vmstat 1
[/cpde]

Disk utilization

iostat -xm 5

Network utilization tool

nicstat

Network connection status

netstat

Note, Windows has the following equivalent command for above purpose

typeperf 
Posted in Linux, Programming | Leave a comment

Java – Recommended GC log options for Java 8 and Java 9

Java 8
-XX:+PrintGCDetails
-XX:+PrintReferenceGC
-XX:+PrintTenuringDistribution
-Xloggc:
-XX:+PrintGCTimeStamps

Java 9
-Xlog:gc*
,gc+ref=debug
,gc+age=trace
:file=
:tags,uptime

Video:
https://www.infoq.com/presentations/java-9-gc?utm_source=presentations_about_GarbageCollection&utm_medium=link&utm_campaign=GarbageCollection

Posted in Java, Programming | Leave a comment

Java – Garbage collectors

1. Serial
-XX:+UseSerialGC JVM

2. Parallel
default

3. CMS(Current Markup Sweep)
-XX:+USeParNewGC

4. G1
–XX:+UseG1GC

Note: Java8 and G1 collector can optimize the string intern with the following option
-XX:+UseStringDeduplicationJVM

More information regarding G1:
https://www.infoq.com/articles/G1-One-Garbage-Collector-To-Rule-Them-All

Posted in Java, Programming | Leave a comment

Java – ZonedDateTime formatter for ISO-8601

Parse example

DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
DateTimeFormatter formatter = builder
        .parseCaseInsensitive()
        .append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
        .appendOffsetId()
        .toFormatter();

ZonedDateTime.parse("2015-08-18T00:00+01:00", formatter);

toString example

DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
DateTimeFormatter formatter = builder
        .parseCaseInsensitive()
        .append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
        .appendOffsetId()
        .toFormatter();

ZonedDateTime zoneTime = new ZonedDatTime();
String date = zoneTime.format(formatter);
Posted in Java, Programming | Leave a comment

Java – heap dump utilities

Heap histogram

jmap -histo [pid]

Heap usage

jmap -heap [pid]
Posted in Java, Programming | Leave a comment

Java – an example code to extract mime type from base64 string of an image


    private boolean isMatch(byte[] pattern, byte[] data) {
        if (pattern.length <= data.length) {
            for (int idx = 0; idx < pattern.length; ++idx) {
                if (pattern[idx] != data[idx])
                    return false;
            }
            return true;
        }

        return false;
    }

    private String getImageType(byte[] data) {
//        filetype    magic number(hex)
//        jpg         FF D8 FF
//        gif         47 49 46 38
//        png         89 50 4E 47 0D 0A 1A 0A
//        bmp         42 4D
//        tiff(LE)    49 49 2A 00
//        tiff(BE)    4D 4D 00 2A

        final byte[] pngPattern = new byte[] { (byte)0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A};
        final byte[] jpgPattern = new byte[] { (byte)0xFF, (byte)0xD8, (byte)0xFF};
        final byte[] gifPattern = new byte[] { 0x47, 0x49, 0x46, 0x38};
        final byte[] bmpPattern = new byte[] { 0x42, 0x4D };
        final byte[] tiffLEPattern = new byte[] { 0x49, 0x49, 0x2A, 0x00};
        final byte[] tiffBEPattern = new byte[] { 0x4D, 0x4D, 0x00, 0x2A};
        if (isMatch(pngPattern, data))
            return "image/png";

        if (isMatch(jpgPattern, data))
            return "image/jpg";

        if (isMatch(gifPattern, data))
            return "image/gif";

        if (isMatch(bmpPattern, data))
            return "image/bmp";

        if (isMatch(tiffLEPattern, data))
            return "image/tif";

        if (isMatch(tiffBEPattern, data))
            return "image/tif";

        return "image/png";
    }

String imageDataBytes = base64String.substring(base64String.indexOf(",") + 1);
                        byte[] bytes = Base64.decodeBase64(imageDataBytes);
                        String imageMimeType = getImageType(bytes);


Posted in Java, Programming | Leave a comment