1 /*
   2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  @test
  26  @summary <rdar://problem/3992478>
  27  @summary Make sure that memory allocated by ByteBuffer.allocateDirect can be reclaimed by the garbage collector
  28  @summary Note that this test starts by allocating very large chunks of memory, so this may disturb other tests slightly
  29  @summary com.apple.junit.java.nio;
  30  @run main AllocateDirect01
  31  */
  32 
  33 import junit.framework.*;
  34 
  35 import java.nio.ByteBuffer;
  36 import java.util.Timer;
  37 import java.util.TimerTask;
  38 
  39 public class AllocateDirect01 extends TestCase {
  40 
  41     // boilerplate
  42     public static Test suite() {
  43         return new TestSuite(AllocateDirect01.class);
  44     }
  45 
  46     public static void main (String[] args) throws RuntimeException {
  47         TestResult tr = junit.textui.TestRunner.run(suite());
  48         if((tr.errorCount() != 0) || (tr.failureCount() != 0)) {
  49             throw new RuntimeException("### Unexpected JUnit errors or failures.");
  50         }
  51     }
  52 
  53     // Use a timer to end the test
  54     volatile boolean endtest = false;
  55     Timer ticktock = null;
  56     protected void setUp() throws Exception {
  57         ticktock = new Timer();
  58 
  59         TimerTask stopper = new TimerTask()        {
  60             public void run() {
  61                 endtest = true;
  62             }
  63         };
  64         ticktock.schedule( stopper, 1500L );
  65     }
  66 
  67     protected void tearDown() throws Exception {
  68         ticktock.cancel();
  69     }
  70 
  71 
  72     // loop, allocating memory we expect to get reclaimed by GC
  73     public void testBuffers() throws Exception {
  74 
  75         // find some suitable size to loop with by using up all of memory
  76         int size = 4;
  77         while (endtest == false) {
  78             try {
  79                 ByteBuffer bb = ByteBuffer.allocateDirect(size);
  80                 assertNotNull(bb);
  81                 size *= 1.5;
  82             }
  83             catch( OutOfMemoryError e) {
  84                 size /= 32;
  85                 break;
  86             }
  87         }
  88 
  89         // We should be able to loop as long as we want, since buf can get reclaimed by gc
  90         while (endtest == false) {
  91           ByteBuffer buf = ByteBuffer.allocateDirect(size);
  92           assertNotNull(buf);
  93         }
  94 
  95     }
  96 }
  97