Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Memory question
#1
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 "libppmac.so" 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)
Else
Return "Library did not initalize"
End If
_CloseLibrary()

If mystatus = 0 Then
Return myresponse.ToString
Else
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?

Thanks.


Update:
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.
Reply
#2
I have to have some other people help me with this who are not available right now. Hopefully tomorrow. Sorry for the delay.
Reply
#3
I'm running a C#.net 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 VB.net 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.

KEJR
Reply
#4
We have this example for C#

http://forums.deltatau.com/showthread.ph...light=mono

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.
Reply
#5
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.
Reply
#6
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.
Reply
#7
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.

Thanks,
Scott

Reply
#8
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.

KEJR
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)