• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 590
  • Last Modified:

JNI + linux + struct sysinfo

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
pquest2003
Asked:
pquest2003
  • 7
  • 6
1 Solution
 
pquest2003Author Commented:
If you need more information please let me know.
0
 
CEHJCommented:
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
 
pquest2003Author Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
CEHJCommented:
Does it return the  same figure every time it's run as a C program?
0
 
pquest2003Author Commented:
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
 
CEHJCommented:
Well, i'd guess that since you're not initializing the struct, you're getting garbage out.
0
 
pquest2003Author Commented:
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
 
CEHJCommented:
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
 
pquest2003Author Commented:
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
 
CEHJCommented:
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
 
pquest2003Author Commented:
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
 
pquest2003Author Commented:
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
 
CEHJCommented:
>>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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now