JNI - Sizeof java object

W

ws_dev2001

Hello all,
I am trying to obtain the size of a java object in C by using JNI. As
we do not have a proper implementation of this in java[other than obj
serialization and reflection etc...], I decided to see if C could
provide me some accurate data.
As in java, C does provide accurate sizeof for primtives and can even
handle String objects received from java. In java reflection, we cannot
find/estimate the length of the string with as much ease as it is in C
via JNI.
Sizeof impl in C seem to be centred around primitives and structs
etc...
So, how do I find the sizeof a java object being sent via native method
call to a C program? Do I need to allocate memory for this object
[estimation based]?
I tried loading the jobject into a struct but sizeof always gives me 4
whether it is via sizeof(jobject) or sizeof(struct myjostruct)!!!
I understand that this is the size allocated for the address in memory.
Can anyone please help ?
TIA
 
C

Chris Smith

So, how do I find the sizeof a java object being sent via native method
call to a C program? Do I need to allocate memory for this object
[estimation based]?
I tried loading the jobject into a struct but sizeof always gives me 4
whether it is via sizeof(jobject) or sizeof(struct myjostruct)!!!
I understand that this is the size allocated for the address in memory.
Can anyone please help ?

The size of a Java object cannot be found in any kind of portable way.
The jobject variable you have is a pointer, so naturally its size is 4
bytes on a 32-bit architecture. It's worth noticing that this isn't
unique to Java; it is similarly not possible to discover the size of a
heap-allocated C struct when all you have is the pointer and you don't
know the struct at compile-time. The sizeof keyword is a compiler
convenience, and is simply not available at run-time.

Sorry, but what you want can't be done.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
H

HK

Hi there.

Hello all,
I am trying to obtain the size of a java object in C by using JNI. As

In jdk-1.5 (aka 5.0) there is

java.lang.instrument.Instrumentation.getObjectSize()
From the docs:

"Returns an implementation-specific approximation
of the amount of storage consumed by the specified object."

For a rough estimator of size in 1.4.2, get yourself my monq.jar
from http://www.ebi.ac.uk/Rebholz-srv/whatizit/software. There is
a class called monq.stuff.Sizeof you may try:
http://www.ebi.ac.uk/Rebholz-srv/whatizit/monq-doc/monq/stuff/Sizeof.html
..

As someone else pointed out, the exact size of data structures
is hard to get hold of, but all we want is a rough estimate
to compare different settings and figure out where the memory
is eaten up.

You may also try to run your program with

java -Xrunhprof

(try -Xrunhprof:help). It dumps a file called java.hprof.txt
at the end of which you find a table of memory usage.

Harald.
 
G

Gordon Beaton

I am trying to obtain the size of a java object in C by using JNI.

Are you just curious, or is there a real problem that this information
will help you solve?

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top