1

While researching on this question: Java: garbage collection for RMI target object? I saw that a full GC is triggered the first time I make this call:

UnicastRemoteObject.exportObject(new Remote(){}, 0);

I ran a very simple program containing the call above, with -verbose:gc set, and consistently saw that a full GC was triggered, e.g. [Full GC 1070K->184K(47552K), 0.0070096 secs]

I ran it through Eclipse, in command line, and on virtual and physical machines. I'm using Sun jdk and hotspot 1.6.

Has anybody observed similar behavior? What can be its cause?

Community
  • 1
  • 1
shrini1000
  • 7,038
  • 12
  • 59
  • 99
  • RMI triggers the GC to ensure that remote objects are cleaned up. i.e. you might not need a clean but there are remote objects proxied in another container which does. How this works has been tuned with different version of Java so I would check you have the latest Java 6 update 35+. This is a comment because I haven't used unicast with RMI. – Peter Lawrey Oct 09 '12 at 07:30
  • @Peter Lawrey, thanks, but this is triggered even if the statement above is the only statement in my main(). There's no RMI registry running or accessed anywhere else. I tried to see the RMI source but couldn't figure out how it happens. I'm wondering - can this be used as a hack to programmatically call GC which will always run... not a good idea I'm sure but something to keep in mind. Btw, I have 1.6.0_31. – shrini1000 Oct 09 '12 at 07:39

2 Answers2

1

There is a background thread which checks that the GC has been run in the java.rmi.dgc.leaseValue which defaults to one hour.

If this is enabled before any GC has run, I suspect it will see this time since the last GC as too long and trigger a full gc.

The class which does this is sun.misc.GC

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Hmm... I saw the source code, and it's basically making a call to System.gc(). My understanding was that calling it may or may not trigger a full gc. Is it somehow changed in Java 6 that it will always get triggered when first called? – shrini1000 Oct 09 '12 at 08:05
  • 1
    It may or may not trigger a GC on different JVMs. The HotSpot JVM has always triggered a full GC by default. This can be changed with command line options. http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html – Peter Lawrey Oct 09 '12 at 08:28
0

Yes, I think, It triggers Full GC.

The distributed garbage collection algorithm used by the RMI system is a reference-counting algorithm. When a client first receives a reference to a remote object, a "referenced" message is sent to the server that is exporting the object. Every subsequent reference within the client's local machine causes a reference counter to be incremented. As a local reference is finalized, the reference count is decremented, and once the count goes to zero, an 'unreferenced' message is sent to the server. Once the server has no more live references to an object and there are no local references, it is free to be finalized and garbage collected.

Also refer below link for Default GC interval lengthed to one hour (6200091) section

http://docs.oracle.com/javase/7/docs/technotes/guides/rmi/relnotes.html

Rahul Agrawal
  • 8,913
  • 18
  • 47
  • 59
  • Not an accurate description. The only reference counting involved happens at the client, and that's just an optimisation. It is primarily a leasing protocol. The 'messages' are actually remote methods, called dirty() and clean(). These are used to obtain, renew, and release 'leases'. The client must keep renewing the lease to prevent it from expiring. – user207421 Oct 10 '12 at 00:13