Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Ahm.<br> (As far as I know) You can't hold the stick from both ends. Either you hold to your information, or you let it go.<br> However... you can hold to some key information that would enable you to finalize. Of course, the key information must be significantly smaller then the "real information" and must not have the real information in its reachable object graph (weak references might help you there).<br> Building on the existing example (pay attention to the key information field):</p> <pre><code>public class Test1 { static class Bloat { // just a heap filler really private double a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z; private final int ii; public Bloat(final int ii) { this.ii = ii; } } // as recommended by Tom Hawtin static class MyReference&lt;T, K&gt; extends SoftReference&lt;T&gt; { private final K keyInformation; MyReference(T referent, K keyInformation, ReferenceQueue&lt;? super T&gt; q) { super(referent, q); this.keyInformation = keyInformation; } public K getKeyInformation() { return keyInformation; } } //...meanwhile, somewhere in the neighbouring galaxy... public static void main(String[] args) throws InterruptedException { ReferenceQueue&lt;Bloat&gt; rq = new ReferenceQueue&lt;Bloat&gt;(); Set&lt;SoftReference&lt;Bloat&gt;&gt; set = new HashSet&lt;SoftReference&lt;Bloat&gt;&gt;(); int i = 0; while (i &lt; 50000) { set.add(new MyReference&lt;Bloat, Integer&gt;(new Bloat(i), i, rq)); final Reference&lt;? extends Bloat&gt; polled = rq.poll(); if (polled != null) { if (polled instanceof MyReference) { final Object keyInfo = ((MyReference) polled).getKeyInformation(); System.out.println("not null, got key info: " + keyInfo + ", finalizing..."); } else { System.out.println("null, can't finalize."); } rq.remove(); System.out.println("removed reference"); } </code></pre> <p>Edit:<br> I want to elaborate on the "either hold your information or let it go". Assuming you had some way of holding to your information. That would have forced the GC to unmark your data, causing the data to actually be cleaned only after you're done with it, in a second GC cycle. This is possible - and its exactly what finalize() is for. Since you stated that you don't want the second cycle to occur, you can't hold your information (if a-->b then !b-->!a). which means you must let it go.</p> <p>Edit2:<br> Actually, a second cycle would occur - but for your "key data", not your "major bloat data". The actual data would be cleared on the first cycle.</p> <p>Edit3:<br> Obviously, the real solution would use a separate thread for removing from the reference queue (don't poll(), remove(), blocking on the dedicated thread).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload