Hi,
   I wrote a code to verify the memory leak problem as following.
C code in so:
void checkJNAMemLeak1(int **head, int *length)
{
    long i = 0;

    *head = (int *)malloc(sizeof(int) * 100000000);
    for(i=0; i<100000000; i++)
    {
        (*head)[i] = 1;
    }

    *length = 100000000;
}

Java code:
        public static void testJNAMemLeak1()
        {
                PointerByReference head = new PointerByReference();
                IntByReference length = new IntByReference();
                
                while(true)
                {
                        libben.checkJNAMemLeak1(head, length);
                        System.out.println(length.getValue());
                        sleep(1);
                        
                }
        }

When we check memory by top command, the virt and res will increase very 
quickly. When we check with jconsole, there is no memory in Java heap. Even I 
execute GC manually by jconsole. Nothing happen.

If I change java code as following:
        public static void testJNAMemLeak1()
        {
                PointerByReference head = new PointerByReference();
                IntByReference length = new IntByReference();
                
                while(true)
                {
                        libben.checkJNAMemLeak1(head, length);
                        System.out.println(length.getValue());
                        sleep(1);
                        
                        libc.free(head.getValue());
                }
        }

        public static void testJNAMemLeak1()
        {
                PointerByReference head = new PointerByReference();
                IntByReference length = new IntByReference();
                
                while(true)
                {
                        libben.checkJNAMemLeak1(head, length);
                        System.out.println(length.getValue());
                        sleep(1);
                        
                        libc.free(head.getValue());
                }
        }

Then everything works well. The virt and res will not increase.
I think we must provide the free functions for all the memory allocated by 
libvirt.

B.R.
Benjamin Wang


-----Original Message-----
From: Benjamin Wang (gendwang) 
Sent: 2012年9月7日 15:22
To: libvir-list@redhat.com
Cc: 'veill...@redhat.com'; Yang Zhou (yangzho)
Subject: RE: Memory free in libvirt JNA

Hi,
    Overview Part of JNA API describes as following:
1. Description1:
If the native method returns char* and actually allocates memory, a return type 
of Pointer should be used to avoid leaking the memory. It is then up to you to 
take the necessary steps to free the allocated memory.

2. Description2:
Declare the method as returning a Structure of the appropriate type, then 
invoke Structure.toArray(int) to convert to an array of initialized structures 
of the appropriate size. Note that your Structure class must have a no-args 
constructor, and you are responsible for freeing the returned memory if 
applicable in whatever way is appropriate for the called function.

And the example code shows as following:
// Original C code
struct Display* get_displays(int* pcount); void free_displays(struct Display* 
displays);

// Equivalent JNA mapping
Display get_displays(IntByReference pcount); void free_displays(Display[] 
displays); ...
IntByReference pcount = new IntByReference(); Display d = 
lib.get_displays(pcount); Display[] displays = 
(Display[])d.toArray(pcount.getValue());
...
lib.free_displays(displays);


That's to say. All the memory allocated by native code must be freed explicitly 
in JNA part. We must add some free memory methods to support the memory-freeing.
Any comments?

B.R.
Benjamin Wang





-----Original Message-----
From: Daniel Veillard [mailto:veill...@redhat.com]
Sent: 2012年8月20日 14:25
To: Benjamin Wang (gendwang)
Cc: st...@tvnet.hu; daniel.schwa...@dtnet.de
Subject: Re: Memory free in libvirt JNA

On Mon, Aug 20, 2012 at 05:15:45AM +0000, Benjamin Wang (gendwang) wrote:
> Hi Veillard,
>   Thanks for your reply. I checked the current Libvirt-JNA 
> implementation. I find that a method named "free" defined in Domain 
> class which is used to free the domain object. If this is mandatory, that's 
> to say, we should a lot of methods into the current Libvirt-jna 
> implementation to free the memory which is allocated by libvirt API. Please 
> correct me!

  As far as I understat free() is aliased as finalize() on that object so the 
java runtime will call free() automatically on garbage collection. I'm not a 
java expert, check some Java litterature for more details about how this is 
done and the cases where


free() might be better called directly.

Daniel

-- 
Daniel Veillard      | libxml Gnome XML XSLT toolkit  http://xmlsoft.org/
dan...@veillard.com  | Rpmfind RPM search engine http://rpmfind.net/ 
http://veillard.com/ | virtualization library  http://libvirt.org/

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

Reply via email to