Github user aledsage commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/331#discussion_r80225179
--- Diff:
utils/common/src/main/java/org/apache/brooklyn/util/javalang/MemoryUsageTracker.java
---
@@ -68,5 +87,76 @@ public long getBytesUsed() {
memoryTrackedReferences.cleanUp();
return bytesUsed.get();
}
+
+ /** forces all soft references to be cleared by trying to allocate an
enormous chunk of memory,
+ * returns a description of what was done
+ * (tune with with {@link #forceClearSoftReferences(long, int)}
+ * for greater than 200M precision in the output message, if you
really care about that) */
+ public static String forceClearSoftReferences() {
+ return forceClearSoftReferences(1000*1000, Integer.MAX_VALUE);
+ }
+ /** as {@link #forceClearSoftReferences()} but gives control over
headroom and max chunk size.
+ * it tries to undershoot by headroom as it approaches maximum (and
then overshoot)
+ * to minimize the chance we take exactly all the memory and starve
another thread;
+ * and it uses the given max chunk size in cases where the caller
wants more precision
+ * (the available memory will be fragmented so the smaller the chunks
the more it can
+ * fill in, but at the expense of time and actual memory provisioned)
*/
+ public static String forceClearSoftReferences(long headroom, int
maxChunk) {
+ final long HEADROOM = 1000*1000;
+ long lastAmount = 0;
+ long nextAmount = 0;
+ long oldUsed = Runtime.getRuntime().totalMemory() -
Runtime.getRuntime().freeMemory();
+ try {
+ List<byte[]> dd = MutableList.of();
+ while (true) {
+ int size =
(int)Math.min(Runtime.getRuntime().freeMemory()-HEADROOM, maxChunk);
+ if (size<HEADROOM) {
+ // do this to minimize the chance another thread gets
an OOME
+ // due to us leaving just a tiny amount of memory
+ size = (int) Math.min(size + 2*HEADROOM, maxChunk);
+ }
+ nextAmount += size;
+ dd.add(new byte[size]);
+ lastAmount = nextAmount;
+ }
+ } catch (OutOfMemoryError e) { /* expected */ }
+ System.gc(); System.gc();
+ long newUsed = Runtime.getRuntime().totalMemory() -
Runtime.getRuntime().freeMemory();
+ return "allocated " +
Strings.makeSizeString((lastAmount+nextAmount)/2) +
+ (lastAmount<nextAmount ? " +-
"+Strings.makeSizeString((nextAmount-lastAmount)/2) : "")
+ +" really free memory in
"+Strings.makeSizeString(maxChunk)+" chunks; "
+ +"memory used from "+Strings.makeSizeString(oldUsed)+" ->
"+Strings.makeSizeString(newUsed)+" / "+
+
Strings.makeSizeString(Runtime.getRuntime().totalMemory());
+ }
+ /** Tracking for soft usage through SoftlyPresent instances */
+ public static class SoftUsageTracker {
+ private Cache<Object,SoftReference<?>> cache = null;
+ public synchronized void enable() {
+ cache = CacheBuilder.newBuilder().weakKeys().build();
+ }
+ public synchronized void disable() {
+ cache = null;
+ }
+ public long getTotalEntries() {
+ return cache.size();
--- End diff --
`return (cache == null ? -1 : cache.size())` (and hope no-one calls
`disable()` concurrently, or that the java threading model messes things up for
you by making the `cache` field not fully initialised from the view point of
this calling thread!). Maybe you should `synchronize` as well.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---