Marshal function leaking memory

P

PBS Productions

Hi,

I hope someone can help with this one. I'm using below function but it
is leaking memory although an article on MSDN says it should not leak
memory.

Dim TopHandle As Integer
TopHandle = GetForegroundWindow()

Dim buffer() As Byte
Dim buffersize As Integer = 250
Dim p As IntPtr = LocalAlloc(&H40, buffersize)

GetWindowText(TopHandle, p, buffersize)

Dim w As String

// Take away below line and the memory leak is gone

w = System.Runtime.InteropServices.Marshal.PtrToStringUni(p)

LocalFree(p)


The marshal function is causing the memory leak. What am I doing
wrong?

Thanks,
Jo.
 
P

PBS Productions

By looking at the memory applet -> available memory. Each time the
functions are called the available memory gets lower. After a while
all memory is used up. If I do the same in eVC++ there's no problem at
all but in VB.NET I'm strugling with the memory leak.

I hope you can help ...

Thanks for the quick reply,

Jo.
 
P

Paul G. Tobey [eMVP]

How much reduction in memory *per call to the Marshal method*?

Paul T.
 
P

PBS Productions

Alex,

try this

While (True)
Dim sbCurrentTitle As New System.Text.StringBuilder(250)
Application.DoEvents()
End While



This eats all memory ????? Am I missing something ??

Thanks,
Jo.
 
P

PBS Productions

Paul,

it's difficult to say but looking at the memory while the function is
call constantly the memory reduce about 20K per second.

Thanks,
Jo.
 
P

PBS Productions

Another one that eats up memory:

Dim SB as System.Text.StringBuilder(250)
Dim S as String

SB.Append("Test")

While true

S = SB.ToString

End While


Is there something wrong with the StringBuilder class?

Thanks,
Jo.
 
A

Alex Yakhnin [MVP]

As far as I can see, every time the ToString() is called the new instance of
the String is created and assigned to your variable S, since it's all
happening inside of the tight loop it nevers gives a chance the GC to clean
up..
 
P

PBS Productions

Alex,

I think that you've got a point there. I gave the GC some time to
cleanup every second or so and most of the memory is cleaned up.
However each time a small portion of the memory is lost. To give some
figures

A loop of 2000 consumes about 200K. After some seconds most of it is
given back but about 4K is still lost and never given back no matter
how long you wait.

Thanks,
Jo.
 
P

Paul G. Tobey [eMVP]

That's a number that sounds like GC overhead (tables of managed object slots
in free memory or something). 4k is probably one page on your hardware and
is the smallest allocation size that you can really have.

Paul T.
 
P

PBS Productions

Is there a way to force a cleanup?

Thanks.


That's a number that sounds like GC overhead (tables of managed object slots
in free memory or something). 4k is probably one page on your hardware and
is the smallest allocation size that you can really have.

Paul T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top