Skip to content

Instantly share code, notes, and snippets.

View rednaxelafx's full-sized avatar

Kris Mok rednaxelafx

View GitHub Profile
@rednaxelafx
rednaxelafx / DirectMemorySize.java
Created January 11, 2012 07:18
An Serviceability-Agent based tool to see stats of NIO direct memory, as an alternative on JDK6 without JMX support for direct memory monitoring. Only works on JDK6; to work on JDK7 will need some tweaking because static variables are moved to Java mirror
import java.io.*;
import java.util.*;
import sun.jvm.hotspot.memory.*;
import sun.jvm.hotspot.oops.*;
import sun.jvm.hotspot.debugger.*;
import sun.jvm.hotspot.runtime.*;
import sun.jvm.hotspot.tools.*;
import sun.jvm.hotspot.utilities.*;
public class DirectMemorySize extends Tool {
@rednaxelafx
rednaxelafx / gist:925323
Last active October 25, 2023 05:30
Correspondence between Sun/Oracle JDK, OpenJDK and HotSpot VM versions

Correspondence between Sun/Oracle JDK, OpenJDK and HotSpot VM versions

build date Sun/Oracle JDK Version OpenJDK Version HotSpot VM Version
2006-11-29 1.6.0-b105 1.6.0-b105
2007-03-14 1.6.0_01-b06 1.6.0_01-b06
2007-06-22 1.6.0_02-b05 1.6.0_02-b05
2007-09-24 1.6.0_03-b05 1.6.0_03-b05
2007-12-14 1.6.0_04-b12 10.0-b19
@rednaxelafx
rednaxelafx / JDK5u22_client.log
Created August 23, 2011 16:43
PrintCompilation samples running Groovy shell 1.7.7 on different versions of HotSpot VM
1 b java.lang.String::charAt (33 bytes)
2 b java.lang.Math::max (11 bytes)
3 b java.util.jar.Manifest$FastInputStream::readLine (167 bytes)
4 b sun.nio.cs.UTF_8$Decoder::decodeArrayLoop (553 bytes)
5 b java.util.Properties$LineReader::readLine (383 bytes)
6 b java.lang.String::hashCode (60 bytes)
7 b java.lang.String::indexOf (151 bytes)
8 b sun.nio.cs.ext.DoubleByteDecoder::decodeSingle (10 bytes)
9 b java.lang.String::lastIndexOf (156 bytes)
10 b java.lang.String::replace (142 bytes)
@rednaxelafx
rednaxelafx / command_prompt
Created January 2, 2012 15:03
quick-n-dirty modification to ObjectHistogram to get generational information
$ export SAPATH=/home/sajia/taobao-jdk-repo/hotspot/make/linux/linux_amd64_compiler2/generated/
$ java -classpath .:$SAPATH/sa-jdi.jar sun.jvm.hotspot.tools.JMap -histo `pgrep java` > jmap_output.log
Attaching to process ID 1587, please wait...
WARNING: Hotspot VM version 20.5-b03 does not match with SA version 20.0-b12-internal. You may see unexpected results.
Debugger attached successfully.
Server compiler detected.
JVM version is 20.5-b03
Iterating over heap. This may take a while...
Heap traversal took 3.577 seconds.
$ java -version
@rednaxelafx
rednaxelafx / demo.groovy
Created April 6, 2011 08:51
get properties from an object in Groovy, even when the object has an getProperties() method
def getPropertiesFrom(obj) {
obj.metaClass.properties.findAll { it.name != 'class' && it.name != 'metaClass' }.inject([:]) { acc, e -> acc[e.name] = e.getProperty(obj); acc }
}
class Foo {
int getBar() { 42; }
List getProperties() { [] }
}
def foo = new Foo();
foo.properties; //=> []
@rednaxelafx
rednaxelafx / ASTDotGenerator.java
Created November 18, 2010 06:36
Generate DOT graphs to visualize a Velocity template's AST. To run this program, the following dependencies are needed: velocity-1.6.4.jar/commons-collections-3.2.1.jar/commons-lang-2.5.jar
import static org.apache.commons.lang.StringEscapeUtils.escapeJava;
import static org.apache.commons.lang.StringUtils.isEmpty;
import java.util.HashMap;
import java.util.Map;
import org.apache.velocity.runtime.parser.node.ASTAddNode;
import org.apache.velocity.runtime.parser.node.ASTAndNode;
import org.apache.velocity.runtime.parser.node.ASTAssignment;
import org.apache.velocity.runtime.parser.node.ASTBlock;
@rednaxelafx
rednaxelafx / DumpClassURL.java
Created December 7, 2011 05:48
Using the ProtectionDomain of an InstanceKlass to see where it was loaded from
import java.io.*;
import java.util.*;
import sun.jvm.hotspot.memory.*;
import sun.jvm.hotspot.oops.*;
import sun.jvm.hotspot.debugger.*;
import sun.jvm.hotspot.runtime.*;
import sun.jvm.hotspot.tools.*;
public class DumpClassURL extends Tool {
public void run() {
@rednaxelafx
rednaxelafx / test_libjit.cc
Last active August 16, 2022 06:36
Comparing different JIT libraries via contrived simple examples
#include <iostream>
// #include <jit/jit-dump.h>
#include <jit/jit-plus.h>
// Create an example function equivalent to:
// int foo(int x, int y, int z) {
// return x + y * z;
// }
jit_function create_example_foo(jit_context& context, int opt_level) {
jit_type_t signature = jit_function::signature_helper(
@rednaxelafx
rednaxelafx / MysteryBox.java
Last active July 10, 2022 06:25
Very simple demo of using CLHSDB from HotSpot Serviceability Agent to inspect Java objects and class metadata
public class MysteryBox {
private long x0, x1;
private boolean y0, y1, y2, y3;
private int z0;
private double[] a = new double[8];
}
@rednaxelafx
rednaxelafx / Demo$$Lambda$1.javap
Created October 12, 2016 08:20
Java 8 serializable lambda pitfall example, https://www.zhihu.com/question/51491241
$ javap -verbose -c -s -l -private Demo\$\$Lambda\$1
Classfile /private/tmp/Demo$$Lambda$1.class
Last modified Oct 12, 2016; size 754 bytes
MD5 checksum 81ce9dafbca34417b2dfa94f596a85a1
final class Demo$$Lambda$1 implements org.apache.spark.api.java.function.VoidFunction
minor version: 0
major version: 52
flags: ACC_FINAL, ACC_SUPER, ACC_SYNTHETIC
Constant pool:
#1 = Utf8 Demo$$Lambda$1