Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Improving JNA Performence

Tags:

java

c

jna

I have the following situtuation on the C side I have a function that builds and returns an array of integers representing RGB values for an image,


  int* pxs(Image* m){
    int* colors = malloc(height * width * sizeof(int));

    //fill the array

    return colors;
   }

On the Java side I retrieve it using,


     //invoke
     Pointer ptr = ref.getPointer();
     int pxs[] = pointer.getIntArray(0, width*height);

     //to an image
     Image img = Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(width, height, pxs, 0 ,width)); 

Then the image gets painted on a panel, from my timing doing all that takes around 50 60 ms, images are from a camera so I get a new one and paint in a loop but after a while (10 secs or so) my machine comes to an halt. I am thinking this is due to garbage collection? so I was wondering if there is a way to fix this?

like image 684
Hamza Yerlikaya Avatar asked Jan 20 '23 18:01

Hamza Yerlikaya


1 Answers

You are never freeing the colors array! Unless JNA does majic, Classic memory leak.

It may be a better idea to pass a ByteBuffer to the native function and have pxs take a char * to be filled with data.

like image 84
KitsuneYMG Avatar answered Jan 23 '23 08:01

KitsuneYMG