473,320 Members | 1,988 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
Jul 17 '05 #1
6 4834
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
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
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
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
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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: parthan | last post by:
We are running our c++ program as Windows services which uses JNI. > Program is getting CLASSPATH env variable correctly and also > initializes JVM successfully. After initializing JVM, programs...
19
by: Dave | last post by:
Hi, I have done some research, trying to Clear The Screen in java code. The first option was the obv: system.out.print("\n\n\n\n\n\n\n\n\n\n\n\n"); then i heard about this method:...
1
by: Thomas Smith | last post by:
I am building a binding to a runtime engine that was written in C++. I have no problems setting up the mapping from object calls to the native implementation, and storing reference to those...
5
by: Mike | last post by:
I need to provide an example to upper management on how this works, but I don't have the time to write one. Does someone already have some code (java and c/vc/vc++) that demonstrates java using...
3
by: Sai Kit Tong | last post by:
I posted for help on legacy code interface 2 days ago. Probably I didn't make it clear in my original mail. I got a couple of answers but none of them address my issues directly (See attached...
1
by: Will Arrowsmith | last post by:
Hi all, I am trying to create a small form much like the Windows Explorer 'open with...' dialog box, which will contain a list of all applications installed on the local machine and their...
7
by: Marcelo | last post by:
Hi, I am getting a very strange error ubuntu:~/docs/jni/jniexamples/chap2/HelloWorld$ gcc -I/usr/local/share/jdk1.5.0_05/include -I/usr/local/share/jdk1.5.0_05/include/linux HelloWorld.c...
5
by: lmttag | last post by:
ASP.NET 2.0 (C#) application Intranet application (not on the Internet) Using Windows authentication and impersonation Windows Server 2003 (IIS6) Server is a member server on a domain Logged...
2
myusernotyours
by: myusernotyours | last post by:
Hi All, Am working on a Java application in which I have to use the JNI to Interface with some native code for both windows and unix. Am using netbeans IDE with the C/C++ pack installed. Am also...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.