Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Memory question
I am trying to develop an HMI using VB.Net and have written a webservice that resides on the PPmac to call methods from the C library "" from a remote web application. What appears to be happening is that when I call a method (from a web app or directly from the service) the amount of memory used by the service increases. After a while the C library cannot initialize. Here is how I have written the calls:

P/Invoke statements to call the C function from the library:
Friend Declare Function _InitLibrary Lib "libppmac" Alias "InitLibrary" () As Integer

Friend Declare Sub _CloseLibrary Lib "libppmac" Alias "CloseLibrary" ()

Friend Declare Ansi Function _GetResponse Lib "libppmac" Alias "GetResponse" (<[In](), MarshalAs(UnmanagedType.LPStr)> ByVal q As String, <Out(), MarshalAs(UnmanagedType.LPStr)> ByVal a As StringBuilder, ByVal maxlen As Int32, ByVal echomode As [Byte]) As Integer

Web service method:
<WebMethod()> _
Public Function GetResponse(ByVal command As String, ByVal echomode As [Byte]) As String
Dim myresponse As New StringBuilder("A", 4096)
Dim mystatus As Integer
Dim initstatus

initstatus = _InitLibrary()
If initstatus = 0 Then
mystatus = _GetResponse(command, myresponse, 255, echomode)
Return "Library did not initalize"
End If

If mystatus = 0 Then
Return myresponse.ToString
Return GetErrorStr(Math.Abs(mystatus))
End If

End Function

I ran one test where all I did was call InitLibrary() and CloseLibrary() and still had the same issue. I will admit that calling unmanaged code from managed code is new to me so I may just be missing something really simple but I can't figure out what it is. Could I not be closing the library properly?


The same thing will happen if I use The <dllImport> attribute instead of the P/Invoke method above and each call consumes approx 16M of memory.
I have to have some other people help me with this who are not available right now. Hopefully tomorrow. Sorry for the delay.
I'm running a program on that PPMAC using mono that queries variables every 100ms currently. I'm not experiencing the memory growth. I'm using DllImport though.

While I don't know a solution to your problem exactly, let me make a few suggestions. I'm not 100% familiar with syntax, so some suggestions might not be spot on:

- Don't initialize the PPMAC library from your method. While I can't say this is a problem, it is certainly inefficient to be calling those functions every time you want to run a Pmac method. I created a static "Pmac" class that gets initialized at program start and closed at program end using methods in that class. Then create wrappers so you can do "Pmac.GetVar()" and "Pmac.GetResponse()" and that kind of thing.

- Check to see if the string that you return every gets cleaned up.

I am not a .net or PPMAC expert, these are just my observations.

We have this example for C#

I have not tested it extensively and I have not heard any complaints. Perhaps looking at it will you figure out what is going wrong since we do not see anything obvious in your code.
Yeah, that was where I started. Maybe Mono and xsp2 don't like the web service side of things. I'm looking into a telnet option too so maybe that will prove to be more fruitful. Thanks for digging into it.
The other thing to try is not to initialize and close the library each time you want to use a method. Initialize it once at the start of the application, and close it when the application closes. You can see if that helps. Maybe the act of repeatedly and quickly opening and closing is causing something to go wrong.
I tried that as well and it actually uses more memory per function call than opening and closing the library for each call (approx 80MB). I'm going to give telnet a shot. It night work out better if I can get the connection figured out.


Have you drilled down to the simplest test example where no dynamic memory allocation is performed? I would do this to start. If you are dealing with an application with all static global classes there really shouldn't be any glaring possibility for memory leaks.


Forum Jump:

Users browsing this thread: 1 Guest(s)