Friday, June 20, 2003

J2SE: Dear SecureRandom why do you do Full GC?

The first time you invoke a random number genareation method on SecureRandom it does about 20 Full GC's. Doesn't Sun know better than this?

The following example code:


public class SecureRandomTest{
public static void main(String args[]) {
SecureRandom s = new SecureRandom();
System.out.println("Before First SecureRandom Call");
(new SecureRandom()).nextInt();
System.out.println("Before Second SecureRandom Call");
(new SecureRandom()).nextInt();
System.out.println("After Second SecureRandom Call");

When executed with Verbose GC options (java -verbose:gc SecureRandomTest) produces the following output:
Before First SecureRandom Call
[Full GC 232K->102K(1984K), 0.0095414 secs]
[Full GC 103K->103K(1984K), 0.0083893 secs]
[Full GC 103K->103K(1984K), 0.0084910 secs]
[Full GC 103K->102K(1984K), 0.0096741 secs]
[Full GC 102K->102K(1984K), 0.0083740 secs]
[Full GC 103K->103K(1984K), 0.0083477 secs]
[Full GC 103K->103K(1984K), 0.0082608 secs]
[Full GC 103K->102K(1984K), 0.0084743 secs]
[Full GC 102K->102K(1984K), 0.0085553 secs]
[Full GC 102K->102K(1984K), 0.0083935 secs]
[Full GC 103K->103K(1984K), 0.0083734 secs]
[Full GC 103K->102K(1984K), 0.0082784 secs]
[Full GC 102K->102K(1984K), 0.0085028 secs]
[Full GC 103K->103K(1984K), 0.0083488 secs]
[Full GC 103K->103K(1984K), 0.0083399 secs]
[Full GC 103K->102K(1984K), 0.0084595 secs]
[Full GC 102K->102K(1984K), 0.0084740 secs]
[Full GC 103K->103K(1984K), 0.0083002 secs]
[Full GC 103K->103K(1984K), 0.0084044 secs]
Before Second SecureRandom Call
After Second SecureRandom Call.

Update 1:
Steven Pozarycki at BEA reponded:

I saw your WebLog and your question about SecureRandom and GC. I recently
had the same issue but I was using a JSP with the same SecureRandom class.
Anyway this is a known issue with Sun and they said it will be addressed in
1.3.1_09 (available in a couple of weeks). The Sun Bug number is 4298667
although you can't view it on

I also tried this on 1.4.1_02 and there is no problem, so Sun addressed
this issue there as well.

I hope that helps!

Update 2:
I had a change to test this with Weblogic8.1Sp1 (With J2SE1.4.1_03). This seems to be fixed as mentioned by Steven Pozarycki.