<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://jogamp.org/bugzilla/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.2"
          urlbase="https://jogamp.org/bugzilla/"
          
          maintainer="sgothel@jausoft.com"
>

    <bug>
          <bug_id>981</bug_id>
          
          <creation_ts>2014-02-22 19:56:27 +0100</creation_ts>
          <short_desc>com.jogamp.opencl.[Low|High]LevelBindingTest fail on OS X 10.6 with java.lang.OutOfMemoryError: Direct buffer memory</short_desc>
          <delta_ts>2014-02-25 02:48:15 +0100</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>3</classification_id>
          <classification>JogAmp</classification>
          <product>Jocl</product>
          <component>opencl</component>
          <version>1</version>
          <rep_platform>All</rep_platform>
          <op_sys>macosx</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>---</priority>
          <bug_severity>normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Wade Walker">wwalker3</reporter>
          <assigned_to name="Wade Walker">wwalker3</assigned_to>
          <cc>sgothel</cc>
          
          <cf_type>---</cf_type>
          <cf_scm_refs>af62da5b7ee3d99da7dc9364f1240fa7a8f5968e</cf_scm_refs>
          <cf_workaround>---</cf_workaround>

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>3771</commentid>
    <comment_count>0</comment_count>
    <who name="Wade Walker">wwalker3</who>
    <bug_when>2014-02-22 19:56:27 +0100</bug_when>
    <thetext>It looks like these tests allocate large direct buffers, and the system just doesn&apos;t do GC quickly enough to prevent these errors. The stack trace is:

java.lang.OutOfMemoryError: Direct buffer memory
	at java.nio.Bits.reserveMemory(Bits.java:632)
	at java.nio.DirectByteBuffer.&lt;init&gt;(DirectByteBuffer.java:97)
	at java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:288)
	at com.jogamp.common.nio.Buffers.newDirectByteBuffer(Buffers.java:73)
	at com.jogamp.opencl.HighLevelBindingTest.vectorAddGMTest(HighLevelBindingTest.java:323)

I propose using something like this to deallocate the large (~10MB) direct buffers created in these tests:

    /**
     * Uses reflection to call the &quot;clean&quot; method of a DirectByteBuffer.
     * Needed because the JVM may not clean these often or quickly enough.
     * Uses reflection because this interface isn&apos;t public, so may
     * break if the class internals change.
     *
     * @param bb The ByteBuffer to cleaned. Does nothing if it&apos;s not direct. 
     */
	public static void cleanDirectByteBuffer(ByteBuffer bb) {

        if(!bb.isDirect())
		    return;

        try {
		    // get cleaner
		    Method cleanerMethod = bb.getClass().getMethod(&quot;cleaner&quot;);
		    cleanerMethod.setAccessible(true);
		    Object cleaner = cleanerMethod.invoke(bb);
		  
		    // invoke clean method
		    Method cleanMethod = cleaner.getClass().getMethod(&quot;clean&quot;);
		    cleanMethod.setAccessible(true);
		    cleanMethod.invoke(cleaner);
	    }
	    catch( Exception e ) {
	    	System.err.println( &quot;Failed to clean DirectByteBuffer. Java implementation details may have changed.&quot; );
	    }
    }</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>3772</commentid>
    <comment_count>1</comment_count>
    <who name="Wade Walker">wwalker3</who>
    <bug_when>2014-02-22 20:09:10 +0100</bug_when>
    <thetext>Or, alternately, I could test how much direct memory is available, then make sure to use less than that. Originally I thought that perhaps the tests were failing because they were all running in one JVM, and old allocations were piling up, but it could just be that there&apos;s only 32MB of direct memory available in the JVM, and the test tries to allocate 3 buffers of 11MB each, and fails on the third one.

I&apos;ll try this first, since it seems more plausible.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>3773</commentid>
    <comment_count>2</comment_count>
    <who name="Wade Walker">wwalker3</who>
    <bug_when>2014-02-22 21:46:29 +0100</bug_when>
    <thetext>Submitted pull request to fix this: https://github.com/JogAmp/jocl/pull/4
Here&apos;s my branch: https://github.com/WadeWalker/jocl/tree/fix_jocl_bug_981_out_of_direct_buffer_memory

Both these tests pass on Windows 7 x86 with -XX:MaxDirectMemorySize=16m set to simulate the problem we&apos;re seeing on the older Mac JVM.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>3774</commentid>
    <comment_count>3</comment_count>
    <who name="Sven Gothel">sgothel</who>
    <bug_when>2014-02-23 00:00:14 +0100</bug_when>
    <thetext>(In reply to comment #2)
&gt; Submitted pull request to fix this: https://github.com/JogAmp/jocl/pull/4
&gt; Here&apos;s my branch:
&gt; https://github.com/WadeWalker/jocl/tree/
&gt; fix_jocl_bug_981_out_of_direct_buffer_memory
&gt; 
&gt; Both these tests pass on Windows 7 x86 with -XX:MaxDirectMemorySize=16m set
&gt; to simulate the problem we&apos;re seeing on the older Mac JVM.

Nice!

Merged.

If you have pushed a commit and since you are the maintainer,
pls add the git-sha1 to the SCM-Refs field so we can query the commit in bugzilla
and find it&apos;s discussion.

.. did it here:

+++

52a618fa844fa19dce19e18c527991ef422b1c43
    Fix memory problems in High/LowLevelBindingTests.
    
    These tests now adaptively reduce the global work size until they
    successfully allocate memory for their DirectByteBuffers. This
    makes the tests work on JVMs where XX:MaxDirectMemorySize is
    smaller than the modern defaults. These tests were failing on
    OS X 10.6 for this reason.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>3775</commentid>
    <comment_count>4</comment_count>
    <who name="Wade Walker">wwalker3</who>
    <bug_when>2014-02-23 00:24:42 +0100</bug_when>
    <thetext>Will do -- I didn&apos;t realize I had the privileges to push to the repo myself. That&apos;s why I was submitting everything to you as pull requests :)</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>3779</commentid>
    <comment_count>5</comment_count>
    <who name="Sven Gothel">sgothel</who>
    <bug_when>2014-02-23 00:51:47 +0100</bug_when>
    <thetext>(In reply to comment #4)
&gt; Will do -- I didn&apos;t realize I had the privileges to push to the repo myself.
&gt; That&apos;s why I was submitting everything to you as pull requests :)

Since you are now a JOCL maintainer .. it&apos;s setup properly now,
i.e. we both can push to jocl* to allow us to ease pushing things fwd.

Ofc course .. if any of us is uncertain about a commit, 
we may refrain from pushing into the master branch and use a 
dedicated one for review ..</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>3786</commentid>
    <comment_count>6</comment_count>
    <who name="Wade Walker">wwalker3</who>
    <bug_when>2014-02-25 02:45:38 +0100</bug_when>
    <thetext>Merged in the fix after testing successfully on Mac OS X, Windows 7, Ubuntu 12.04, and Oracle Solaris 11.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>3787</commentid>
    <comment_count>7</comment_count>
    <who name="Wade Walker">wwalker3</who>
    <bug_when>2014-02-25 02:48:15 +0100</bug_when>
    <thetext>Oops, last comment and SCM ref were for bug 978... changing it back.</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>