This Question is Assumed Answered

1 "correct" answer available (4 pts) 2 "helpful" answers available (2 pts)
3 Replies Last post: Dec 3, 2009 1:28 AM by Caleb Jeffery  
Caleb Jeffery   8 posts since
Nov 26, 2009
Currently Being Moderated

Dec 2, 2009 9:01 AM

Memory issue

Hi I have an issue in regards to memory allocation and freeing.

 

I am running the following code in a thread

public synchronized void run() {
        // int ss = -1;
        int count = 0;
        UInt32 numBytesInQueue = new UInt32(0);
        UInt32 queueStatus = new UInt32(USBXpressDLL.SI_SUCCESS);
        UInt32 numBytesToRead = new UInt32();
        AnsiString bs = new AnsiString();
        Pointer nbq = new Pointer(numBytesInQueue);
        Pointer qs = new Pointer(queueStatus);
        Pointer nbr = new Pointer(numBytesToRead);
        Pointer.Void voidptr = new Pointer.Void(0);
        byte[] databyteArray;
        // Step 1: get a Runtime object
 
        while (true) {
            if (this.readEnabled) {
 
 
                int ss = USBXpressDLL.SI_CheckRXQueue(this.handle, nbq, qs); //This function will start the memory jump
 
 
                ss = 1;
                if (ss == USBXpressDLL.SI_SUCCESS) {
                    if ((int) queueStatus.getValue() == USBXpressDLL.SI_RX_OVERRUN) {
                        ss = USBXpressDLL.SI_FlushBuffers(this.handle, (byte) 1, (byte) 1);
                        if (ss != USBXpressDLL.SI_SUCCESS) {
                            try {
                                throw new USBXpressException("Error Flushing Buffers"); //$NON-NLS-1$
                            } catch (USBXpressException e) {
                                e.printStackTrace();
                            }
                        }
                    } else {
                        if ((int) queueStatus.getValue() == USBXpressDLL.SI_RX_READY) {
                            bs = new AnsiString((int) numBytesInQueue.getValue());
                            DataBuffer ba;
                            ss = USBXpressDLL.SI_Read(this.handle, new Pointer(bs), numBytesInQueue, nbr, voidptr);
 
                            if (ss == USBXpressDLL.SI_SUCCESS) {
                                ba = bs.getDataBuffer();
                                databyteArray = ba.readByteArray(0, (int) (numBytesToRead.getValue()) - 1);
                                this.dispatch(databyteArray);
                            } else {
                                try {
                                    throw new USBXpressException("Error Reading"); //$NON-NLS-1$
                                } catch (USBXpressException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                        ;
                    }
                    ;
                } else {
                    try {
                        throw new USBXpressException("Error Reading RX Queue"); //$NON-NLS-1$
                    } catch (USBXpressException e) {
                        e.printStackTrace();
                    }
                }
            }
            try {
                Thread.sleep(this.readInterval);
            } catch (InterruptedException e) {
 
            }
            databyteArray = null;
        }
    }

This is calling the following functions

 

// function SI_CheckRXQueue (Handle: THandle; var NumBytesInQueue: dword;
    // var QueueStatus: dword): TSI_Status; stdcall external 'SiUSBXp.dll';
    public static int SI_CheckRXQueue(Handle handle, Pointer pointer, Pointer pointer2) {
        // System.out.println("SI_CheckRXQueue");
        int i = 0;
        Function add = USBXPRESS_LIB.getFunction(Messages.getString("USBXpressDLL.11")); //$NON-NLS-1$
        Int result = new Int();
        i = (int) result.getValue();
        add.invoke(result, handle, pointer, pointer2);
        add = null;
        result = null;
        add = null;
        return printReturnCode(i);
    }
 
 
    // function SI_FlushBuffers (Handle: THandle; FlushTransmit: byte;
    // FlushReceive: byte): TSI_Status; stdcall external 'SiUSBXp.dll';
    public static int SI_FlushBuffers(Handle handle, byte flushTransmit, byte flushReceive) {
        // System.out.println("SI_FlushBuffers");
        Function add = USBXPRESS_LIB.getFunction(Messages.getString("USBXpressDLL.13")); //$NON-NLS-1$
        Int result = new Int();
 
        add.invoke(result, handle, new UInt32(flushTransmit), new UInt32(flushReceive));
 
        int i = (int) result.getValue();
        add = null;
        result = null;
        return printReturnCode(i);
    }
 
 
    // function SI_Read (Handle: THandle; Buffer: LPVOID; NumBytesToRead: dword;
    // var NumBytesReturned: dword; lpOverlapped: POverlapped): TSI_Status;
    // stdcall external 'SiUSBXp.dll';
    public static int SI_Read(Handle handle, Pointer Buffer_LPVOID, UInt32 numBytesToRead, Pointer pointer2, Void void1) {
        // System.out.println("SI_Read");
        Function add = USBXPRESS_LIB.getFunction(Messages.getString("USBXpressDLL.15")); //$NON-NLS-1$
        Int result = new Int();
        Parameter[] params = { handle, Buffer_LPVOID, numBytesToRead, pointer2, void1 };
        add.invoke(result, params);
        // add.invoke(result,Handle_THandle,Buffer_LPVOID,NumBytesToWrite_dword,NumBytesReturned_dword,lpOverlapped_POverlapped);
        int i = (int) result.getValue();
        add = null;
        result = null;
        return printReturnCode(i);
    }

 

I am succesfully reading data from USBXpress device but I am noticing a considerable jump(16K-100K) in memory every time I run this thread. Which by the way is being called every 20ms. which means approx 1MB per minute.

 

I need to be able to run this thread over 1-2weeks. but since I am having this memory issue the thread would only run overnight.

 

I would love your input on the matter.

 

I have been stumped on how I would fix this issue as I am not sure if this is a memory leak in java or the dll.

Serge Piletsky TeamDev Ltd. 1,066 posts since
Apr 24, 2006
Currently Being Moderated
1. Dec 2, 2009 4:05 PM in response to: Caleb Jeffery
Re: Memory issue

Hi,

 

It's quite difficult to diagnose such issue having only the those code snippets, which actually are not compilable. I have also found several issues in it, please take a look:

 

1) Getting a result value before a function call:

screen1.png

 

2) Using wrong java.lang.Void type in the implementation of SI_Read method. Of course that not compilable at all, but even if it could, then it would produce the runtime error:

screen2.png

3) the method run() has several warnings in it:

screen3.png

So I am wondering how did you manage to run that code at all and which IDE allowed to run the code that had such obvious mistakes in it.

 

Also, the calling of SI_CheckRXQueue function looks very simple and it should not cause any memory leak issues, unless that function itself has such memory issue in it. Therefore, please make sure that you are using that API correctly.

 

-Serge

Serge Piletsky TeamDev Ltd. 1,066 posts since
Apr 24, 2006
Currently Being Moderated
2. Dec 2, 2009 4:15 PM in response to: Serge Piletsky
Re: Memory issue

Hi,

 

Please ignore my previous questions about uncompilable parts of code - I found the reason myself.

 

Nevertheless the question about using API still remains.

 

-Serge

More Like This

  • Retrieving data ...