Skip to content

Instantly share code, notes, and snippets.

Avatar

Andrei Pangin apangin

View GitHub Profile
View SymbolReader.java
import sun.misc.Unsafe;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
View LongArrayCompare.java
package bench;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import java.util.Arrays;
import java.util.concurrent.ThreadLocalRandom;
@apangin
apangin / MapBench.java
Created Jul 29, 2021
ConcurrentHashMap vs. HashMap performance
View MapBench.java
package bench;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import java.util.HashMap;
import java.util.Map;
View StringBuilderHash.java
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
// See https://twitter.com/tagir_valeev/status/1402089474805354499
public class StringBuilderHash {
View ParallelStream.java
import java.util.ArrayList;
import java.util.concurrent.locks.LockSupport;
import java.util.stream.StreamSupport;
public class ParallelStream {
public static int[] process(Iterable<Integer> iterable) {
return StreamSupport.stream(iterable.spliterator(), true)
.mapToInt(i -> {
LockSupport.parkNanos(1_000_000);
@apangin
apangin / VMFlags.java
Created Jan 27, 2021
Read and modify non-manageable JVM flags in runtime: macOS version
View VMFlags.java
import sun.misc.Unsafe;
import javax.management.ObjectName;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
@apangin
apangin / LogSafepoints.java
Last active Jan 15, 2021
Java safepoint event listener
View LogSafepoints.java
import one.log.event.ILoggingEvent;
import one.log.event.OperationLogEvent;
import one.log.stat.StatCollector;
import one.log.stat.Statistics;
import one.nio.mgt.DiagnosticCommand;
import one.nio.os.BatchThread;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.regex.Matcher;
@apangin
apangin / vmoption.c
Created Jan 15, 2021
Change non-manageable JVM option in runtime
View vmoption.c
#include <jvmti.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
JNIEXPORT jint JNICALL
Agent_OnAttach(JavaVM* vm, char* options, void* reserved) {
if (options != NULL) {
int* addr = (int*)strtol(options, NULL, 0);
printf("vmoption [%p] = %d\n", addr, *addr); fflush(stdout);
@apangin
apangin / unmalloc.c
Created Jan 11, 2021
Avoid long TTSP pauses caused by Unsafe.allocateMemory/freeMemory
View unmalloc.c
// Replaces implementation of Unsafe.allocateMemory/freeMemory
// to avoid long time-to-safepoint pauses.
//
// Compile: gcc -O3 -fno-omit-frame-pointer -fPIC -shared -olibunmalloc.so unmalloc.c
//
// Usage: java -agentpath:/path/to/libunmalloc.so ...
#include <jvmti.h>
#include <stdint.h>
#include <stdio.h>
@apangin
apangin / ChangeVMFlag.java
Created Dec 11, 2020
Change MaxJavaStackTraceDepth JVM flag in runtime
View ChangeVMFlag.java
import sun.jvm.hotspot.debugger.Address;
import sun.jvm.hotspot.runtime.VM;
import sun.jvm.hotspot.tools.Tool;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* Changes -XX:MaxJavaStackTraceDepth flag to 1000000 in runtime
* without restarting the JVM.