G1 does not handle soft links

Here is my simple gc test:

public class Main {

  static class Data {
    public long[] l = new long[100];
  }

  static List<SoftReference<Data>> list = new ArrayList<>();

  public static void main(String[] args) {
    long i = 0;

    while (true) {
      list.add(new SoftReference<>(new Data()));
      ++i;
      if (i % 1000 == 0) {
        sleep(1);
        if (i % 1_000_000 == 0)
          sleep(1000);
      }
    }
  }

  static void sleep(long millis) {
    try { Thread.sleep(millis); } catch (InterruptedException ignored) {}
  }
}

      

Using these arguments (G1 included):

java -Xmx2G -Xms2G -XX:MaxPermSize=128m -XX:+PrintGCDetails -XX:+PrintGCTimeStamps 
-XX:+PrintAdaptiveSizePolicy -Xloggc:jvm.log -XX:+UseG1GC 
-XX:InitiatingHeapOccupancyPercent=5 Main

      

I grep the output:

grep -E "(Full|GC cleanup)" jvm.log

      

and you get something like this:

0.564: [GC cleanup 277M->277M(2048M), 0.0009922 secs]
0.879: [GC cleanup 443M->442M(2048M), 0.0009396 secs]
1.676: [GC cleanup 859M->856M(2048M), 0.0008681 secs]
3.530: [GC cleanup 1324M->1320M(2048M), 0.0012422 secs]
4.838: [GC cleanup 1711M->1707M(2048M), 0.0010601 secs]
6.334: [Full GC 2047M->102M(2048M), 1.2659685 secs]
8.322: [GC cleanup 534M->534M(2048M), 0.0009528 secs]
11.250: [GC cleanup 1460M->1450M(2048M), 0.0011207 secs]
13.499: [Full GC 2046M->512M(2048M), 1.3534848 secs]

      

All ParallelGc collections seem to have soft links collected, and concurrent collections were almost useless. Dropping the heap from VisualVm also confirms this version.

Am I missing something or is this a bug in the G1?

Tested on 1.7.0_51-b13 and 1.8.0_45-b15 x64.

+3


source to share


2 answers


Perhaps you are confused about weak links?

The GC is not forced to collect soft references unless it is under strong memory pressure.

See here for details .

In particular, note the following quote from the documentation:



Soft reference objects that are cleaned up at the discretion of the garbage collector in response to memory requirements.

The only real guarantee offered by the documentation is:

All soft references to soft-reach objects are guaranteed to be cleared before the virtual machine throws an OutOfMemoryError.

+3


source


There is -XX:SoftRefLRUPolicyMSPerMB=

(default = 1000) that determines the speed of soft link collection. Lower values ​​make them collect them faster.

I don't know how it interacts with the G1 regions. It is possible that some regions are rarely touched and therefore their soft links are not taken into account.

As @sstan mentioned, WeakReferences can offer slightly more predictable behavior at the cost of much shorter link lifetime.




Another problem is that you are not clearing the list of soft offer objects. If you register refs with a refs queue , poll that queue during post sleep (1000) for refs and remove them from the list, you shouldn't run out of memory. For faster searches, dialing may be more convenient than a list.

+1


source







All Articles