By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,470 Members | 1,907 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,470 IT Pros & Developers. It's quick & easy.

JNI: Local Reference Management

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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:
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


Jul 17 '05 #2

P: n/a
er***********@hotmail.com (Eric) wrote in message news:<66**************************@posting.google. com>...
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

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
Jul 17 '05 #3

P: n/a
Exemple:
char[] a = new char[] {'a','b','c','d','e','f'};
char[] b = a;
MyClass.getReferenceCount( a ); // Suppose to be 2
char[] c = a;
MyClass.getReferenceCount( a ); // Suppose to be 3
...
I thinked it was by using the JNI_N_REFS_IN_USE(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_frame, jobj);
}

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

Thanks for your help.

dy******@hotmail.com (Jared Dykstra) wrote in message news:<ba**************************@posting.google. com>...
er***********@hotmail.com (Eric) wrote in message news:<66**************************@posting.google. com>...
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

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

Jul 17 '05 #4

P: n/a
er***********@hotmail.com (Eric) wrote in message news:<66**************************@posting.google. com>...
Exemple:
char[] a = new char[] {'a','b','c','d','e','f'};
char[] b = a;
MyClass.getReferenceCount( a ); // Suppose to be 2
char[] c = a;
MyClass.getReferenceCount( a ); // Suppose to be 3
...


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
http://www.bork.org/~jared
Jul 17 '05 #5

P: n/a
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:
Exemple:
char[] a = new char[] {'a','b','c','d','e','f'};
char[] b = a;
MyClass.getReferenceCount( a ); // Suppose to be 2
char[] c = a;
MyClass.getReferenceCount( a ); // Suppose to be 3
...
I thinked it was by using the JNI_N_REFS_IN_USE(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_frame, jobj);
}

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

Thanks for your help.

dy******@hotmail.com (Jared Dykstra) wrote in message news:<ba**************************@posting.google. com>...
er***********@hotmail.com (Eric) wrote in message news:<66**************************@posting.google. com>...
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

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


Jul 17 '05 #6

P: n/a
> 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.
Yes I would care about the actual count.


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.


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.substring() 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.
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.