According to JNI docs, GetStringCritical()
disable garbage collection while you hold on to some memory managed by the JVM. Using this instead of GetString()
puts your native at risks of deadlock if you call into the Java layer or perform blocking operation before ReleaseStringCritical()
is invoked. So what's the benefit of the Critical function, and if it's synonomous, disabling garbage collection while you access JVM managed memory?
views:
26answers:
1First a correction: according to the specs, the GetXxxCritical()
methods may disable garbage collection.
So what's the benefit of the Critical function,
The advantage of using the GetXxxCritical()
methods is that they are more likely to return a pointer to the original data rather than a copy of the data. This means that they are likely to be faster. Of course, this comes at a cost in terms of the documented restrictions / caveats.
... and is it synonymous with disabling garbage collection while you access JVM managed memory?
Well no; see my correction. The methods may disable garbage collection, or they may not. It will depend on how your platform implements the JNI interfaces, and possibly on other things such as JVM garbage collector options.