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.


