Skip to content

Instantly share code, notes, and snippets.

@nhachicha
nhachicha / Main.kt
Created February 6, 2020 12:08
Example of a GC detection mechanism in Kotlin/Native
package sample
import kotlinx.cinterop.StableRef
import kotlinx.cinterop.toCPointer
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.ReceiveChannel
import kotlin.native.internal.GC
import kotlin.native.ref.WeakReference
@nhachicha
nhachicha / DumpRealms.java
Created July 19, 2017 14:29
Dump Realms configuration and SyncUser Using Reflection
public static void dumpAllRealms() {
try {
StringBuilder report =
new StringBuilder("*******************************************************************************\n")
.append("********* D U M P I N G R E A L M I N S T A N C E S [ S T A R T ] **********\n")
.append("*******************************************************************************\n\n");
int nbrRealm = 0;
Class<?> realmCacheClazz = Class.forName("io.realm.RealmCache");
java.lang.reflect.Method getConfigurationMethod = realmCacheClazz.getMethod("getConfiguration");
@nhachicha
nhachicha / Synchronizers.java
Created October 29, 2015 16:32
Examples using synchronizers (CountDownLatch, CyclicBarrier, Phaser)
// ********************************************************************* //
// ************************** CountDownLatch ************************** //
// ********************************************************************* //
public class RandomIntAverage {
CountDownLatch controller = new CountDownLatch(NB_THREADS);
public void randomIntAvg() throws InterruptedException {
for (int i = 0; i < NB_THREADS; i++) {
new Thread(new Task()).start();
}
@nhachicha
nhachicha / UnitTestWithLooper.java
Last active November 10, 2015 16:56
Example demonstrating problem you may encounter using the Looper/Handler with unit tests. & a pattern at the end to avoid them
public void testLooperNotProcessingMessageTestWillNotExit () throws InterruptedException {
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
assertEquals(42, msg.what);
}
};
boolean messageSent = handler.sendEmptyMessage(42);
assertTrue(messageSent);
@nhachicha
nhachicha / PhantomReferenceLeakDetector.java
Last active December 21, 2017 23:43
Example demonstrating how to use PhantomReference to detect memory leak
static class Activity {
interface Listener {}
Service service;
Activity(Service service) {
this.service = service;
}
void onStart() {
service.registerListener(new Listener() {});//Listener hold a reference to Activity
package com.nabilhachicha.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import com.squareup.okhttp.Cache;
import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.OkHttpClient;