Skip to content

Commit 8944da6

Browse files
Unfinished
1 parent 143571f commit 8944da6

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

build.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,7 @@ cd ..
12401240
<!-- enable JNI on JDK 24+ -->
12411241
<jvmarg value="--enable-native-access=ALL-UNNAMED" />
12421242
<jvmarg if:set="test.jdwp" value="${test.jdwp}" />
1243+
<jvmarg value="-Xmx64m" />
12431244
<!-- optionally run headless -->
12441245
<syspropertyset refid="headless"/>
12451246
<!-- avoid VM conflicts with JNA protected mode -->

lib/native/linux-x86-64.jar

2.39 KB
Binary file not shown.

src/com/sun/jna/internal/Cleaner.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public Cleanable register(Object obj, Runnable cleanupTask) {
8585
}
8686

8787
private CleanerRef add(CleanerRef ref) {
88+
runCleanup();
8889
map.put(ref, ref);
8990
cleanerThreadLock.readLock().lock();
9091
try {
@@ -109,6 +110,16 @@ private CleanerRef add(CleanerRef ref) {
109110
return ref;
110111
}
111112

113+
private void runCleanup() {
114+
if (Boolean.valueOf(System.getProperty("jna.inlinecleanup", "false"))) {
115+
for (Reference<? extends Object> cref = referenceQueue.poll(); cref != null; cref = referenceQueue.poll()) {
116+
if (cref instanceof CleanerRef) {
117+
((CleanerRef) cref).clean();
118+
}
119+
}
120+
}
121+
}
122+
112123
private void remove(CleanerRef ref) {
113124
map.remove(ref);
114125
cleanerThreadLock.readLock().lock();
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.sun.jna.different_package;
2+
3+
import com.sun.jna.Structure;
4+
import junit.framework.TestCase;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class CleanerTest extends TestCase {
10+
private static final int NUM_THREADS = 16;
11+
private static final long ITERATIONS = 10000;
12+
13+
@Structure.FieldOrder({"bytes"})
14+
public static class Dummy extends Structure {
15+
public byte[] bytes;
16+
17+
public Dummy() {}
18+
19+
public Dummy(byte[] what) { bytes = what; }
20+
}
21+
22+
private static class Allocator implements Runnable {
23+
@Override
24+
public void run() {
25+
for (long i = 0; i < ITERATIONS; ++i) {
26+
Dummy d = new Dummy(new byte[1024]);
27+
d.write();
28+
}
29+
}
30+
}
31+
32+
public void testOOM() {
33+
List<Thread> threads = new ArrayList<>(NUM_THREADS);
34+
for (int i = 0; i < NUM_THREADS; ++i) {
35+
Thread t = new Thread(new Allocator());
36+
t.start();
37+
threads.add(t);
38+
}
39+
for (Thread t : threads) {
40+
while (t.isAlive()) {
41+
try {
42+
t.join();
43+
} catch (InterruptedException ignore) {}
44+
}
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)