Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will the Java Garbage Collector stop my POSIX threads created from JNI calls?

I have a time critical application that needs to send a UDP datagram on a set schedule. The tolerance for jitter is very low on the receiving side. Implementing this with a java ScheduledThreadPoolExecutor isn't adequate because when the GC does a "Stop the World" collection my thread pauses while the GC does it's job.

I would like to implement the business logic in Java while implementing the time critical portions with POSIX threads in C++ (Native environment is Linux by the way). This would allow us to save thousands of lines of code written in Java and also get the pacing we need from the native system calls.

My question is this: If I call a JNI function that creates a separate POSIX thread will that thread be "paused" when the Java GC does a "Stop the World" collection? Are there any pitfalls that an experienced JNI guru would like to point out with this approach or any alternative approaches one would suggest?

As always, thanks to the awesome stack overflow community!

like image 960
LPalmer Avatar asked Apr 15 '11 15:04

LPalmer


People also ask

Does garbage collection block threads?

They all block the running threads occasionally when they need to do global GC. The concurrent GC does try to do most of its work in a concurrent fashion though.

Do Java threads get garbage collected?

4) Garbage Collection in Java is carried by a daemon thread called Garbage Collector. 5) Before removing an object from memory garbage collection thread invokes finalize() method of that object and gives an opportunity to perform any sort of cleanup required.

Can a thread object be collected by the garbage collector while running?

No thread (or the things it refers to) will be garbage-collected while it is still running. Save this answer.

What is the purpose of Java garbage collector?

When Java programs run on the JVM, objects are created on the heap, which is a portion of memory dedicated to the program. Eventually, some objects will no longer be needed. The garbage collector finds these unused objects and deletes them to free up memory.


2 Answers

My question is this: If I call a JNI function that creates a separate POSIX thread will that thread be "paused" when the Java GC does a "Stop the World" collection?

It will have no effect. STW affects java threads that needs to arrive to a safe point. A java thread into native code won't be affected either.

like image 150
bestsss Avatar answered Oct 06 '22 21:10

bestsss


It shouldn't block the posix thread (assuming that the gc doesn't use so much cpu that other system calls would be blocked). I would think that it would block access to the posix thread from the java application but only for a very limited time.

like image 25
joekarl Avatar answered Oct 06 '22 22:10

joekarl