-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Now we no longer depend on the GC to free native memory on Linux.
JNA is freeing 192 KiB of per NuProcess native buffers on GC Finalizer / Reference Queue processing. In cases with very low GC activity, this is essentially a memory leak, potentially causing native memory issues. * out/err/in buffers are freed onExit, * event needed for Epoll #registerProcess and ##queueWrite is allocated and freed in the method, * tweak in reusing of IntByReference for duration of epoll/kqueue processor, * updated JNA library to 5.13.0, to get #close on Memory object (added in 5.12.0).
- Loading branch information
Showing
9 changed files
with
141 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.zaxxer.nuprocess.internal; | ||
|
||
import com.sun.jna.Memory; | ||
import com.sun.jna.Pointer; | ||
import com.sun.jna.PointerType; | ||
|
||
public class Util { | ||
public static void close(PointerType pointerType) { | ||
if (pointerType == null) { | ||
return; | ||
} | ||
close(pointerType.getPointer()); | ||
} | ||
|
||
public static void close(Pointer p) { | ||
if (p instanceof Memory) { | ||
((Memory) p).close(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.zaxxer.nuprocess; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
/** | ||
* Yet another threaded test. | ||
*/ | ||
public class ThreadLoadTest { | ||
|
||
@Test | ||
public void testStartLoad() throws InterruptedException { | ||
int durationInMs = 15_000; | ||
long cutOfTime = System.currentTimeMillis() + durationInMs; | ||
int nrOfThreads = (Runtime.getRuntime().availableProcessors() * 2); | ||
CountDownLatch latch = new CountDownLatch(nrOfThreads); | ||
AtomicLong runCountCode0 = new AtomicLong(); | ||
AtomicLong runCountCodeNon0 = new AtomicLong(); | ||
AtomicLong problems = new AtomicLong(); | ||
for (int i = 0; i < nrOfThreads; i++) { | ||
startNewThread(latch, cutOfTime, runCountCode0, runCountCodeNon0, problems); | ||
} | ||
|
||
Assert.assertTrue(latch.await(durationInMs + 1_000, TimeUnit.MILLISECONDS)); | ||
System.out.println("runCount 0 = " + runCountCode0.get()); | ||
System.out.println("runCount non-0 = " + runCountCodeNon0.get()); | ||
System.out.println("problems = " + problems.get()); | ||
} | ||
|
||
private void startNewThread(final CountDownLatch latch, final long cutOfTime, final AtomicLong zeroExit, final AtomicLong nonZeroExit, AtomicLong problems) { | ||
new Thread(new Runnable() { | ||
public void run() { | ||
while (System.currentTimeMillis() < cutOfTime) { | ||
final int randomInt = ThreadLocalRandom.current().nextInt(10_000); | ||
final String text = "foo" + randomInt; | ||
long startTime = System.nanoTime(); | ||
|
||
final NuProcess start = new NuProcessBuilder(new NuAbstractProcessHandler() { | ||
public void onPreStart(final NuProcess nuProcess) { | ||
super.onPreStart(nuProcess); | ||
} | ||
|
||
@Override | ||
public void onStart(NuProcess nuProcess) { | ||
} | ||
|
||
@Override | ||
public void onStdout(ByteBuffer buffer, boolean closed) { | ||
|
||
} | ||
|
||
public void onExit(final int statusCode) { | ||
if (statusCode == 0) { | ||
zeroExit.incrementAndGet(); | ||
} else { | ||
nonZeroExit.incrementAndGet(); | ||
} | ||
} | ||
}, "echo", text).start(); | ||
try { | ||
start.waitFor(10, TimeUnit.DAYS); | ||
|
||
// System.out.println("Took " + ((System.nanoTime() - startTime) / 1000000)); | ||
// start.wantWrite(); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
latch.countDown(); | ||
} | ||
}).start(); | ||
} | ||
} |