Posts

Observability of the Java Virtual Machine

Image
The JVM is one of the most observable runtimes. It provides us lots of tools for troubleshooting a JVM application in production. 1. Thread observability Threads are how the JVM actually does work. When something is wrong in production, the symptom is almost always a thread: stopped, blocked, leaking etc. Thread dumps work on any JVM with no  instrumentation, no agents, no restarts. <Example project link with /threaddump endpoint>         // (1) Deadlock — two threads grab the same pair of locks in opposite order.         new Thread(() -> grab(LOCK_A, LOCK_B), "deadlock-A-then-B").start();         new Thread(() -> grab(LOCK_B, LOCK_A), "deadlock-B-then-A").start(); http://localhost:8080/actuator/threaddump To list the JVMS, we can use the command below. PS C:\observe-jvm> jps -lv 25296 jdk.jcmd/sun.tools.jps.Jps -Dapplication.home=C:\Program Files\Microsoft\jdk-21.0.3.9-hotspot -Xms8m -Djdk.module.main=...

DEVOPS

Image
This table shows imporant aspects of DEVOPS and related software products. The ones that I have used in the past are painted in red. I think this is a very nice table to look at alternative solutions to use for new projects. Of course some of them are essential: Version control. (Subversion,  Git, ..) Build tool. (Maven, Gradle, ..) Testing. Unit Test, Stress Test, UI TEst. (JUnit, JMeter, Selenium) Continuous Integration. (Bamboo, Continuum, ..) The others seem to depend on the size of the organization and the project. Some use AWS, others manage their own servers. 

Do you speak MATLAB?

MATLAB is a programming platform designed specifically for engineers and scientists. I first used it for a masters degree course on image processing. And now, I'm looking at it for the purpose of machine learning. Now lets remember what we can do with it: Matlab has a command line and also you can save your scripts as .m files. You should define your functions and save them as .m files. Note: If you put a semicolon to the end of the expression, the output is not shown in the console. Lets create a 3 x 3 matrix: A = [1, 2, 3; 0, 0, 0; -1, -1, -1] Lets create a vector: V = [2, 3, 6, 7] Lets create a column vector: V = [2; 3; 4; 5] Lets draw a diagram with some values: X = [1,2,3,4,5]; Y= [0,-1,3,4,8]; plot(X,Y) How to define a function? function y = myFunction(x)     y = x^2 + x + 1; end From command line: result = myFunction(6); Lets create a uniformly spaced vector.  V1 = 1:10 V2 = 1:10 plot(V1, V2) We can also specify the int...

More fun with left shift operator

Lets say we want to have sum of two integers. But we are not allowed to use + and - operators. Below is a way to do that: public class Solution {     public static int getSum(int a, int b) {         if (a > 0 && b > 0) {             return positiveSum(a, b);         }         if (a < 0 && b < 0) {             return negate(positiveSum(Math.abs(a), Math.abs(b)));         }         if (a > 0 && b < 0) {             return minus(a, Math.abs(b));         } else {             return minus(b, Math.abs(a));  ...

Using XOR operator in bit manipulation problems

Problem description from LeetCode: Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. Input: [1,2,1,3,2,5]  Output: [3,5] Solution: If we apply XOR to all the elements, the result is the XOR of the lonely integers. The next step is finding the two integers that produce the XOR result.  To do that, we can use this trick: The set bits of A XOR B  gives us which bits are different in A and B After finding the last different set bit, we can dissect the numbers from the input.  Source Code: class Solution {         public static int[] singleNumber(int[] nums) {         int xor = 0;         for (int num : nums) {             xor ^= num;         }         int lastSetBit = lastSetBit(x...

Java Bit Manipulation For Repeated DNA Sequences

Problem description from LeetCode: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA. Write a function to find all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule. Example: Input: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT" Output: ["AAAAACCCCC", "CCCCCAAAAA"] Solution: The idea is that we can use 2 bits to store a letter.  We use a map to count occurences of 10 letter sequences. When we get a new letter, we shift 2 bits to the left and then append the new letter. After appending 11th character, we must remove the first char on the left. To remove it, since we store 10 letters in 20 bits, our bit mask is: 0xFFFFF Source Code: public class RepeatedDNA {     public static List findRepeatedDnaSequences(String string) {         if (strin...

UTF-8 Validation in Java

A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules: For 1-byte character, the first bit is a 0, followed by its unicode code. For n-bytes character, the first n-bits are all one's, the n+1 bit is 0, followed by n-1 bytes with most significant 2 bits being 10. Given an array of integers representing the data, lets calculate whether it is a valid utf-8 encoding. Example: data = [235, 140, 4], which represented the octet sequence: 11101011 10001100 00000100.  Return false.  The first 3 bits are all one's and the 4th bit is 0 means it is a 3-bytes character. The next byte is a continuation byte which starts with 10 and that's correct. But the second continuation byte does not start with 10, so it is invalid. Source Code: public static void main(String[] args) {         // should be true         System.out.println(validUtf8(new int[]{228, 189, 160, 229, 165, 189, 13, 10})); ...

Notes on Bit Manipulation

Finding nth bit of an integer: If you want to get the nth bit of a number, use this mask: 1 << n So if you left shift 1 n times, you get the mask. Below is the expression to find the nth bit: number & (1 << n)  Setting the nth bit to 1: number | (1 << n) Clearing the nth bit (setting it to zero): We want to apply & operator with 0 to the nth bit. The mask we need is: ~(1 << n) So the expression to clear the nth bit is: number & ~(1 << n) Flipping the nth bit: We want to apply XOR with 1. Below is the expression: number ^ (1 << n) Finding if a bit in a position is set: One way to do that is to right shift the number n times. Than the nth bit is the first bit. The following expression is the answer: (number >> n) & 1 Finding number of 1 bits in unsigned integer Loop through the bits and check. (Look at hammingWeight() method code below) Finding Hamming Distance The Hamming d...

Popular posts from this blog

The WeakReference class, monitoring memory leak and garbage collection in a Java application

Simplescalar Simulator - Part 2: sim-outorder.c

Kotlin Language Features Related to Null Handling