Skip to content

Instantly share code, notes, and snippets.

@lukhnos
lukhnos / ThrowableLeaks.java
Last active September 4, 2015 15:34
Demonstrates that Throwable leaks memory in j2objc, see https://github.com/google/j2objc/issues/601
import com.google.j2objc.annotations.AutoreleasePool;
public class ThrowableLeaks {
public static void main(String args[]) {
for (int i = 0; ; i++) {
foo(i);
bar(i);
try {
Thread.sleep(1000);
} catch (Exception e) {
@lukhnos
lukhnos / NIOLeaks.java
Last active September 4, 2015 15:34
Demonstrates that FileChannelImpl and FileChannel leak memory in j2objc, see https://github.com/google/j2objc/issues/603
import com.google.j2objc.annotations.AutoreleasePool;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
public class NIOLeaks {
public static void main(String args[]) {
@lukhnos
lukhnos / FastEnumGotcha1.java
Created September 9, 2015 14:55
A case where you can't fast-enumerate an Iterable, 1/3
// This works perfectly as a Java program, but produces wrong output when
// translated into Objective-C. Can you spot why?
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class FastEnumGotcha {
static class Wrapper<T> {
T value;
@lukhnos
lukhnos / FastEnumGotcha2.java
Created September 9, 2015 14:56
A case where you can't fast-enumerate an Iterable, 2/3
// Solution 1: Use annotation.
import com.google.j2objc.annotations.LoopTranslation;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class FastEnumGotcha {
static class Wrapper<T> {
T value;
@lukhnos
lukhnos / FastEnumGotcha3.java
Created September 9, 2015 14:56
A case where you can't fast-enumerate an Iterable, 3/3
// Solution 2: Use OCNI.
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class FastEnumGotcha {
static class Wrapper<T> {
T value;
@lukhnos
lukhnos / ReferenceQueueLeaks.java
Created December 9, 2015 17:37
ReferenceQueue leaks demo
/**
* This demonstrates the leaks in ReferenceQueue.
*
* To run the demo, compile the source with:
*
* j2objc ReferenceQueueLeaks.java
* j2objcc ReferenceQueueLeaks.m
*
* Then use the Leaks tool in Instruments and have it run "a.out ReferenceQueueLeaks".
*/