TestClosureGC.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* Verify that libffi closures aren't deallocated too early.
  2. Copyright (C) 2007 Free Software Foundation, Inc
  3. Contributed by Alexandre Oliva <aoliva@redhat.com>
  4. If libffi closures are released too early, we lose.
  5. */
  6. import java.util.HashSet;
  7. public class TestClosureGC {
  8. public static String objId (Object obj) {
  9. return obj + "/"
  10. + Integer.toHexString(obj.getClass().getClassLoader().hashCode());
  11. }
  12. public static class cld extends java.net.URLClassLoader {
  13. static final Object obj = new cl0();
  14. public cld () throws Exception {
  15. super(new java.net.URL[] { });
  16. /* System.out.println (objId (this) + " created"); */
  17. }
  18. public void finalize () {
  19. /* System.out.println (objId (this) + " finalized"); */
  20. }
  21. public String toString () {
  22. return this.getClass().getName() + "@"
  23. + Integer.toHexString (hashCode ());
  24. }
  25. public Class loadClass (String name) throws ClassNotFoundException {
  26. try {
  27. java.io.InputStream IS = getSystemResourceAsStream
  28. (name + ".class");
  29. int maxsz = 1024, readsz = 0;
  30. byte buf[] = new byte[maxsz];
  31. for(;;) {
  32. int readnow = IS.read (buf, readsz, maxsz - readsz);
  33. if (readnow <= 0)
  34. break;
  35. readsz += readnow;
  36. if (readsz == maxsz) {
  37. byte newbuf[] = new byte[maxsz *= 2];
  38. System.arraycopy (buf, 0, newbuf, 0, readsz);
  39. buf = newbuf;
  40. }
  41. }
  42. return defineClass (name, buf, 0, readsz);
  43. } catch (Exception e) {
  44. return super.loadClass (name);
  45. }
  46. }
  47. }
  48. public static class cl0 {
  49. public cl0 () {
  50. /* System.out.println (objId (this) + " created"); */
  51. }
  52. public void finalize () {
  53. /* System.out.println (objId (this) + " finalized"); */
  54. }
  55. }
  56. public static class cl1 {
  57. final HashSet hs;
  58. static final Object obj = new cl0();
  59. public cl1 (final HashSet hs) {
  60. this.hs = hs;
  61. /* System.out.println (objId (this) + " created"); */
  62. }
  63. public void finalize () {
  64. /* System.out.println (objId (this) + " finalized"); */
  65. }
  66. }
  67. public static class cl2 {
  68. final HashSet hs;
  69. static final Object obj = new cl0();
  70. public cl2 (final HashSet hs) {
  71. this.hs = hs;
  72. /* System.out.println (objId (this) + " created"); */
  73. }
  74. public void finalize () {
  75. /* System.out.println (objId (this) + " finalized"); */
  76. hs.add(this);
  77. hs.add(new cl0());
  78. }
  79. }
  80. static final HashSet hs = new HashSet();
  81. static final Object obj = new cl0();
  82. public static void main(String[] argv) throws Exception {
  83. {
  84. Class[] hscs = { HashSet.class };
  85. Object[] hsos = { hs };
  86. new cld().loadClass ("TestClosureGC$cl1").
  87. getConstructor (hscs).newInstance (hsos);
  88. new cld().loadClass ("TestClosureGC$cl2").
  89. getConstructor (hscs).newInstance (hsos);
  90. new cld().loadClass ("TestClosureGC$cl1").
  91. getConstructor (hscs).newInstance (hsos);
  92. new cld().loadClass ("TestClosureGC$cl1").
  93. getConstructor (hscs).newInstance (hsos);
  94. }
  95. for (int i = 1; i <= 5; i++) {
  96. /* System.out.println ("Will run GC and finalization " + i); */
  97. System.gc ();
  98. Thread.sleep (100);
  99. System.runFinalization ();
  100. Thread.sleep (100);
  101. if (hs.isEmpty ())
  102. continue;
  103. java.util.Iterator it = hs.iterator ();
  104. while (it.hasNext ()) {
  105. Object obj = it.next();
  106. /* System.out.println (objId (obj) + " in ht, removing"); */
  107. it.remove ();
  108. }
  109. }
  110. System.out.println ("ok");
  111. }
  112. }