Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Virtual Machine / March 2005

Tip: Looking for answers? Try searching our database.

JNI: Java objects to C to estimate size

Thread view: 
ws_dev2001@yahoo.com - 28 Feb 2005 08:41 GMT
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
Boudewijn Dijkstra - 07 Mar 2005 22:11 GMT
> Hello all,
> I am trying to obtain the size of a java object in C by using JNI. As
[quoted text clipped - 5 lines]
> find/estimate the length of the string with as much ease as it is in C
> via JNI.

What is wrong with the length() method of String?

> Sizeof impl in C seem to be centred around primitives and structs
> etc...

The results of the sizeof operator are calculated by the C compiler.  That is
inherent to the C language.  Know your languages.

> 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]?

If the object exists in Java, memory is already allocated for it.

> 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.

Have you looked at the declaration of the jobject struct/typedef?

To solve your problem, you could dive deeply into the source code of your JVM,
trying to find out how it keeps track of object size.  The easy way would of
course be reflection.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.