JNI: Local Reference Management

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eric

    JNI: Local Reference Management

    Hi,

    After reading the JNI specs, it looks like no JNI call is available to
    retrieve the reference count for a specific Java Object (LocalRef or
    GlobalRef).

    I would like to able to retrieve the "reference count", is it
    possible? Without using JVMPI or JVMDI ...

    Thanks
  • Brian S O'Neill

    #2
    Re: JNI: Local Reference Management

    I do not believe that JNI LocalRef and GlobalRef have reference counts.
    If you're trying to implement a reference counting garbage collector for
    LocalRefs and GlobalRefs, you'll have to create your own reference count
    and store it in the Java object that you're referencing.

    Eric wrote:[color=blue]
    > Hi,
    >
    > After reading the JNI specs, it looks like no JNI call is available to
    > retrieve the reference count for a specific Java Object (LocalRef or
    > GlobalRef).
    >
    > I would like to able to retrieve the "reference count", is it
    > possible? Without using JVMPI or JVMDI ...
    >
    > Thanks[/color]

    Comment

    • Jared Dykstra

      #3
      Re: JNI: Local Reference Management

      eric_beaumier@h otmail.com (Eric) wrote in message news:<6619720f. 0401050455.6a4d fcfd@posting.go ogle.com>...[color=blue]
      > Hi,
      >
      > After reading the JNI specs, it looks like no JNI call is available to
      > retrieve the reference count for a specific Java Object (LocalRef or
      > GlobalRef).
      >
      > I would like to able to retrieve the "reference count", is it
      > possible? Without using JVMPI or JVMDI ...
      >
      > Thanks[/color]


      I'm not sure exactly what you are trying to do, but using JNI should
      not be something you do if it can be avoided. Can the functionality
      you are looking for be acheived through the java.lang.ref package? It
      provides limited interaction with the GC.



      ---
      Jared Dykstra

      Comment

      • Eric

        #4
        Re: JNI: Local Reference Management

        Exemple:
        char[] a = new char[] {'a','b','c','d ','e','f'};
        char[] b = a;
        MyClass.getRefe renceCount( a ); // Suppose to be 2
        char[] c = a;
        MyClass.getRefe renceCount( a ); // Suppose to be 3
        ...


        I thinked it was by using the JNI_N_REFS_IN_U SE(frame) in jni.c ?

        In fact it's the same logic as GC do, even if it check only if a class
        is finalyzable ... but take a look in jni.c.


        jobject jni_mkRefLocal( ExecEnv *ee, JHandle *jobj){
        return (jobject) jni_addRef(ee->current_fram e, jobj);
        }

        static JHandle **jni_addRef(Ja vaFrame *frame, JHandle *handle){
        ....
        }



        Thanks for your help.



        dyksjare@hotmai l.com (Jared Dykstra) wrote in message news:<ba84b53e. 0401061221.10bc 1aae@posting.go ogle.com>...[color=blue]
        > eric_beaumier@h otmail.com (Eric) wrote in message news:<6619720f. 0401050455.6a4d fcfd@posting.go ogle.com>...[color=green]
        > > Hi,
        > >
        > > After reading the JNI specs, it looks like no JNI call is available to
        > > retrieve the reference count for a specific Java Object (LocalRef or
        > > GlobalRef).
        > >
        > > I would like to able to retrieve the "reference count", is it
        > > possible? Without using JVMPI or JVMDI ...
        > >
        > > Thanks[/color]
        >
        >
        > I'm not sure exactly what you are trying to do, but using JNI should
        > not be something you do if it can be avoided. Can the functionality
        > you are looking for be acheived through the java.lang.ref package? It
        > provides limited interaction with the GC.
        >
        > http://java.sun.com/j2se/1.4.2/docs/...e-summary.html
        >
        > ---
        > Jared Dykstra
        > http://bork.org/~jared[/color]

        Comment

        • Jared Dykstra

          #5
          Re: JNI: Local Reference Management

          eric_beaumier@h otmail.com (Eric) wrote in message news:<6619720f. 0401070236.550c d355@posting.go ogle.com>...[color=blue]
          > Exemple:
          > char[] a = new char[] {'a','b','c','d ','e','f'};
          > char[] b = a;
          > MyClass.getRefe renceCount( a ); // Suppose to be 2
          > char[] c = a;
          > MyClass.getRefe renceCount( a ); // Suppose to be 3
          > ...
          >[/color]

          Do you care about the actual count, or only if it is zero or not? At
          the java level, it's always non-zero, otherwise there would be no way
          to access the object.

          Any time you use JNI, you defeat Java's portability by introducing a
          dependancy on machine specific code. Your application will now
          require native C code to be compiled into a shared object and
          installed on that machine or it will not run. There's nothing
          necessarly wrong with that, as long as you are aware of the
          self-imposed limitations.

          Also be aware that many types of references may exist depending on how
          the object is used. This provides more information to the GC as to
          when an object can be dumped, particularly under low memory conditions
          in the JVM. Look at java.lang.ref subclass (soft/weak/phantom)
          documentation for more info.

          Although I'm unaware of any Java API to do what you're looking for,
          try invoking referencequeue. toString() and see if you get anything
          useful.

          ---
          Jared Dykstra

          Comment

          • Brian S O'Neill

            #6
            Re: JNI: Local Reference Management

            If this is what you're trying to do, it isn't going to work. There is no
            reference count. JNI performs special reference management when moving
            between Java-land and native-land, but if it has any reference count at
            all, it would only be for JNI references to Java objects.

            If you want to know more how Java reclaims objects without reference
            counts, I suggest reading up on how garbage collection works. If you
            want to perform some special action when an object is collected, look
            into creating a finalizer or a java.lang.ref.< xxx>Reference.

            Eric wrote:[color=blue]
            > Exemple:
            > char[] a = new char[] {'a','b','c','d ','e','f'};
            > char[] b = a;
            > MyClass.getRefe renceCount( a ); // Suppose to be 2
            > char[] c = a;
            > MyClass.getRefe renceCount( a ); // Suppose to be 3
            > ...
            >
            >
            > I thinked it was by using the JNI_N_REFS_IN_U SE(frame) in jni.c ?
            >
            > In fact it's the same logic as GC do, even if it check only if a class
            > is finalyzable ... but take a look in jni.c.
            >
            >
            > jobject jni_mkRefLocal( ExecEnv *ee, JHandle *jobj){
            > return (jobject) jni_addRef(ee->current_fram e, jobj);
            > }
            >
            > static JHandle **jni_addRef(Ja vaFrame *frame, JHandle *handle){
            > ...
            > }
            >
            >
            >
            > Thanks for your help.
            >
            >
            >
            > dyksjare@hotmai l.com (Jared Dykstra) wrote in message news:<ba84b53e. 0401061221.10bc 1aae@posting.go ogle.com>...
            >[color=green]
            >>eric_beaumier @hotmail.com (Eric) wrote in message news:<6619720f. 0401050455.6a4d fcfd@posting.go ogle.com>...
            >>[color=darkred]
            >>>Hi,
            >>>
            >>>After reading the JNI specs, it looks like no JNI call is available to
            >>>retrieve the reference count for a specific Java Object (LocalRef or
            >>>GlobalRef) .
            >>>
            >>>I would like to able to retrieve the "reference count", is it
            >>>possible? Without using JVMPI or JVMDI ...
            >>>
            >>>Thanks[/color]
            >>
            >>
            >>I'm not sure exactly what you are trying to do, but using JNI should
            >>not be something you do if it can be avoided. Can the functionality
            >>you are looking for be acheived through the java.lang.ref package? It
            >>provides limited interaction with the GC.
            >>
            >>http://java.sun.com/j2se/1.4.2/docs/...e-summary.html
            >>
            >>---
            >>Jared Dykstra
            >>http://bork.org/~jared[/color][/color]

            Comment

            • Eric

              #7
              Re: JNI: Local Reference Management

              > Do you care about the actual count, or only if it is zero or not? At[color=blue]
              > the java level, it's always non-zero, otherwise there would be no way
              > to access the object.[/color]

              Yes I would care about the actual count.

              [color=blue]
              >
              > Any time you use JNI, you defeat Java's portability by introducing a
              > dependancy on machine specific code. Your application will now
              > require native C code to be compiled into a shared object and
              > installed on that machine or it will not run. There's nothing
              > necessarly wrong with that, as long as you are aware of the
              > self-imposed limitations.[/color]

              Already know that and I haven't problem to be platform specific (using
              JNI API).


              The final issue to do this, was again for exemple, when you manipulate
              String.
              When you call a String.substrin g() you create a new instance of
              String, but you reuse the same value[] char array object as the
              original. Only the offset and count differ ... And I exclude the
              ..intern() reference used a lot in Java.
              This mean for many instances of String object we can have the same
              char[] object reference.

              I was interested to have the current reference count from one specific
              object (here the value[] char array for one specific String instance).
              But if it's not possible ...

              Thanks.

              Comment

              Working...