?
Solved

JNI +  linux  + struct sysinfo

Posted on 2003-03-03
13
Medium Priority
?
588 Views
Last Modified: 2013-11-23
OK,I have a java program that is using jni.This program is made for linux and uses the "struct sysinfo" to retrieve the totalram size and some other stuff.Ok,here is the simple c  method/lib

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <jni.h>
#include "linux/kernel.h"
#include <sys/sysinfo.h>
#include "D_0005fRamGraph.h"

JNIEXPORT jlong JNICALL
Java_D_1RamGraph_totalRam(JNIEnv *eve, jobject ob) {
  struct sysinfo Sysinfo;
   
 
 sysinfo (&Sysinfo);


long ramsize = Sysinfo.totalram;


 

  return ramsize;
}

Now this returns the totalram size just fine,the problem is with Sysinfo.freeram.If I use freeram inplace of totalram it does not return the right information.It returns 788 when it should be only 121 cause that is what the c program outputs if it is ran just as a c program.Any ideas why this would be returning that high of a number when it should be much smaller since I only have 194mb ram.Thanks for your time and help  
0
Comment
Question by:pquest2003
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 

Author Comment

by:pquest2003
ID: 8062278
If you need more information please let me know.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 150 total points
ID: 8063305
You might be better off switching topic until you've got the API thing sorted out, as i can't see that the anomaly you mention is likely to have anything to do with its place inside a JNI function.
0
 

Author Comment

by:pquest2003
ID: 8063567
Ok can anyone tell me if they see anything wrong with this code.


The java code

public class ramtest {

     public native long testing();
     

    static {
        System.loadLibrary("ramtest");
    }
   public ramtest(){
    System.out.println(testing());
 }
 public static void main(String[] args){
  ramtest te = new ramtest();
  System.out.println(te.testing());
 }
}




The c code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <jni.h>
#include "linux/kernel.h"
#include <sys/sysinfo.h>
#include "ramtest.h"

JNIEXPORT jlong JNICALL
Java_ramtest_testing(JNIEnv *eve, jobject ob) {
 struct sysinfo Sysinfo;
  sysinfo (&Sysinfo);

  long ramsize = Sysinfo.freeram;

 return ramsize;
}

Does anyone see anything wrong with this????I can't see why this is not returning the right information.Thanks for your time and help.


0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 86

Expert Comment

by:CEHJ
ID: 8063589
Does it return the  same figure every time it's run as a C program?
0
 

Author Comment

by:pquest2003
ID: 8063727
Well now as I am retesting the c program it is doing the same darn thing.I would not think it should be doing this I would think it should be returning  a amout less then the ram size,what do you think.Do you think it is returning the vm memory ??


here is the c function
  #include <sys/sysinfo.h>

  int sysinfo(struct sysinfo *info);



here is the struct sysinfo;

         struct sysinfo {
                   long uptime;             /* Seconds since boot */
                   unsigned long loads[3];  /* 1, 5, and 15 minute load average\s */
                   unsigned long totalram;  /* Total usable main memory size */
                   unsigned long freeram;   /* Available memory size */
                   unsigned long sharedram; /* Amount of shared memory */
                   unsigned long bufferram; /* Memory used by buffers */
                   unsigned long totalswap; /* Total swap space size */
                   unsigned long freeswap;  /* swap space still available */
                   unsigned short procs;    /* Number of current processes */
                   char _f[22];             /* Pads structure to 64 bytes */
              };
   
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8063748
Well, i'd guess that since you're not initializing the struct, you're getting garbage out.
0
 

Author Comment

by:pquest2003
ID: 8064014
Do you have a solution??,so I could fix this.Or help me understand what I am doing wrong cause as far as I can see I am setting the c function up right and  to retrieve the freeram.But as I said I think it should be returning a amount smaller then the totalram size,unless the freeram is the vn memory.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8064088
If you're getting roughly the same figure each time, you are probably calling it right, but i'm afraid I don't know what it's *supposed* to return. Try experimenting with setting heap sizes on the command line and see if it has any effect.
0
 

Author Comment

by:pquest2003
ID: 8064196
you are probably calling it right, but i'm afraid I don't know what it's *supposed* to return.

Well I was thinking it suppose to return the amount of freeram.So if I only had 194mb of ram it would return something like 100mb or 135mb etc of ram,depending on how many other programs I am  running.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8064240
Well, in these days of virtual memory, it doesn't surprise me. Does it change when you alter the heap settings?

You can probably disregard my earlier comment as the call should fill the struct properly i'd imagine.
0
 

Author Comment

by:pquest2003
ID: 8064559
Have not done the heap setting yet but I will check it out.I think I see where I was going wrong it was cause of the way I was thinking the freeram should work,when it does not work that way.Anyway back to the drawing board.Thanks for your time and help
0
 

Author Comment

by:pquest2003
ID: 8065880
how you get more points around here with out buying answering ? or making a new account.Do you just  wait  for your points to build up again??Once again thanks for your reply
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8065909
>>how you get more points around here with out buying answering

I'm not sure as i've not used that many of mine yet fortunately ;-) Posting a question to Community Support for 0 points would be one way to find out if you can't see the answer.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month12 days, 1 hour left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question