
public final class JNIMemoryManager extends Object
Native memory isn't nicely garbage collected like Java memory is, and
managing it can be challenging to those not familiar with it. Ferry is all
about making native objects behave nicely in Java, and in order to have Ferry
make objects look like Java objects, the JNIMemoryManager does
some black magic to ensure native memory is released behind the scenes.
To do this by default Ferry uses a Robust mechanism for ensuring native memory is released, but that comes at the expense of some Speed. This approach is tunable though.
if you run a Java Profiler and see your application is spending a lot of time
copying on incremental collections, or you need to eke out a few more
microseconds of speed, or you're bored, then it's time to experiment with
different JNIMemoryManager.MemoryModel configurations that Ferry supports by calling
setMemoryModel(MemoryModel). This is pretty advanced stuff though,
so be warned.
Read JNIMemoryManager.MemoryModel for more.
JNIMemoryManager.MemoryModel| Modifier and Type | Class and Description |
|---|---|
static class |
JNIMemoryManager.MemoryModel
The different types of native memory allocation models Ferry supports.
|
| Modifier and Type | Method and Description |
|---|---|
static void |
collect()
|
void |
dumpMemoryLog()
Dump the contents of our memory cache to the log.
|
void |
finalize()
A finalizer for the memory manager itself.
|
void |
flush()
Internal Only.
|
void |
gc()
Do a Ferry Garbage Collection.
|
void |
gc(boolean doSweep)
Does a Ferry Garbage Collection, and also sweeps our internal
JNIReference heap to remove any lightweight references we may
have left around. |
double |
getExpandIncrement()
Get the percentage value we will increment the reference cache by
if we need to expand it.
|
double |
getMaxFreeRatio()
Get the maximum ratio of free space we'll allow in a memory manager heap
before trying to shrink on the next collection.
|
static JNIMemoryManager.MemoryModel |
getMemoryModel()
Get the
JNIMemoryManager.MemoryModel that Ferry is using for allocating large memory
blocks. |
static JNIMemoryManager |
getMgr()
Get the global
JNIMemoryManager being used. |
double |
getMinFreeRatio()
Gets the minimum ratio of free space to total memory manager heap
size we'll allow before expanding the heap.
|
int |
getMinimumReferencesToCache()
Get the minimum number of references to cache.
|
long |
getNumPinnedObjects()
Get the number of Ferry objects we believe are still in use.
|
double |
getShrinkFactor()
Get the shrink factor.
|
boolean |
isMemoryDebugging()
Will object allocations contain debug information when allocated?
|
void |
setExpandIncrement(double expandIncrement)
Get the percentage value we will increment the reference cache by
if we need to expand it.
|
void |
setMaxFreeRatio(double maxFreeRatio)
Sets the maximum ratio of free space we'll allow without
trying to shrink the memory manager heap.
|
void |
setMemoryDebugging(boolean value)
Set whether the
JNIMemoryManager should cause objects
to be allocated with debugging information. |
static void |
setMemoryModel(JNIMemoryManager.MemoryModel model)
Sets the
JNIMemoryManager.MemoryModel. |
void |
setMinFreeRatio(double minFreeRatio)
Sets the minimum ratio of free space to total memory manager heap
size we'll allow before expanding the heap.
|
void |
setMinimumReferencesToCache(int minimumReferencesToCache)
Sets the minimum number of references to cache.
|
void |
setShrinkFactor(double shrinkFactor)
Set the percentage value we will shrink the reference cache by when
we determine shrinking is possible.
|
void |
startCollectionThread()
Starts a new Ferry collection thread that will wake up whenever a memory
reference needs clean-up from native code.
|
void |
stopCollectionThread()
Stops the Ferry collection thread if running.
|
public static JNIMemoryManager getMgr()
JNIMemoryManager being used.public static void collect()
public void setMinimumReferencesToCache(int minimumReferencesToCache)
The JNIMemoryManager needs to cache weak references
to allocated Ferry object. This setting controls the minimum
size of that cache.
minimumReferencesToCache - Minimum number of references to cache.IllegalArgumentException - if <= 0public int getMinimumReferencesToCache()
setMinimumReferencesToCache(int)public void setExpandIncrement(double expandIncrement)
expandIncrement - A percentage we will increment the reference cache
by if we need to expand it.IllegalArgumentException - if <= 0public double getExpandIncrement()
setExpandIncrement(double)public void setShrinkFactor(double shrinkFactor)
If we decide to shrink, the amount we shrink the cache by is
getExpandIncrement()*getShrinkFactor().
shrinkFactor - The shrink percentage.IllegalArgumentException - if shrinkFactor <=0 or >= 100.setExpandIncrement(double)public double getShrinkFactor()
setShrinkFactor(double)public void setMaxFreeRatio(double maxFreeRatio)
maxFreeRatio - The maximum amount (0 < maxFreeRatio < 100) of
free space.public double getMaxFreeRatio()
setMaxFreeRatio(double)public void setMinFreeRatio(double minFreeRatio)
minFreeRatio - The minimum free ratio.public double getMinFreeRatio()
setMinFreeRatio(double)public long getNumPinnedObjects()
This may be different than what you think because the Java garbage collector may not have collected all objects yet.
Also, this method needs to walk the entire ferry reference heap, so it can be expensive and not accurate (as the value may change even before this method returns). Use only for debugging.
public void dumpMemoryLog()
This method requires a global lock in order to run so only use for debugging.
public boolean isMemoryDebugging()
setMemoryDebugging(boolean)public void setMemoryDebugging(boolean value)
JNIMemoryManager should cause objects
to be allocated with debugging information. This is false
by default as it causes a slight performance hit per-allocation.
If true, then each allocation after setting to true will remember
the class of each object allocated, and the unique java hash
code (Object.hashCode()) of each object allocated. Then
in calls to dumpMemoryLog(), those classes and hash
values will also be printed.
value - true to turn on memory debugging; false to turn it off.public void finalize()
public void gc()
This takes all Ferry objects that are no longer reachable and deletes the
underlying native memory. It is called every time you allocate a new Ferry
object to ensure Ferry is freeing up native objects as soon as possible
(rather than waiting for the potentially slow finalizer thread). It is also
called via a finalizer on an object that is referenced by the Ferry'ed
object (that way, the earlier of the next Ferry allocation, or the
finalizer thread, frees up unused native memory). Lastly, you can use
startCollectionThread() to start up a thread to call this
automagically for you (and that thread will exit when your JVM exits).
public void gc(boolean doSweep)
JNIReference heap to remove any lightweight references we may
have left around.doSweep - if true, we sweep the heap. This involves a global lock
and so should be used sparingly.public void startCollectionThread()
This thread is not started by default as Ferry calls gc()
internally whenever a new Ferry object is allocated. But if you're
caching Ferry objects and hence avoiding new allocations, you may want to
call this to ensure all objects are promptly collected.
This call is ignored if the collection thread is already running.
The thread can be stopped by calling stopCollectionThread(), and
will also exit if interrupted by Java.
public void stopCollectionThread()
public static JNIMemoryManager.MemoryModel getMemoryModel()
JNIMemoryManager.MemoryModel that Ferry is using for allocating large memory
blocks.JNIMemoryManager.MemoryModelpublic static void setMemoryModel(JNIMemoryManager.MemoryModel model)
JNIMemoryManager.MemoryModel.
Only call once per process; Calling more than once has an unspecified effect.
model - The model you want to use.getMemoryModel(),
JNIMemoryManager.MemoryModelpublic final void flush()
Copyright © 2018 Humble Software. All rights reserved.