LocalFree Fails to Release Memory

G

Giorgos

I allocate 10mb of memory to my program using the following code, which
works fine.

IntPtr[] p = new IntPtr[10000];

for (int k = 0; k < p.Length; k++)
{

p[k] = LocalAlloc(LMEM_FIXED, 1024);

}


Then, I try to release the memory using the code below. The problem is
the memory does not get free until I exit my application.


for (int j = 0; j < p.Length; j++)
{
p[j] = LocalFree(p[j]);

}

I don't understand what I am doing wrong. I hope someone can help me
with this problem. Thanks !
 
P

Paul G. Tobey [eMVP]

Looks right to me, but LocalFree *does* work, in general (or the operating
system would only run for a second or two before it crashed; *everything*
uses it). Show us your declarations for those P/Invokes...

Paul T.
 
G

Giorgos

Thanks for your time !! The P/Invoke declarations are:

public const uint LMEM_FIXED = 0x0000;

[DllImport("coredll.dll")]
extern public static IntPtr LocalAlloc(uint uFlags, uint
uBytes);

[DllImport("coredll.dll")]
extern public static IntPtr LocalFree(IntPtr hMem);

I noticed that if I use

p[k] = LocalAlloc(LMEM_FIXED, 1024 * 1024);

eg. Allocate 1mb each step instead of 1 kb, the memory is released
properly, but this is not what I want to do :(. I am testing this on a
Windows mobile 5 operating system.

This is for a little application that is part of my task manager.
It's something really simple and it seems to work ok except of this.
If you need any more info please ask me.

Thank you again and I hope you can help me !
 
P

Paul G. Tobey [eMVP]

It sounds like your heap might be getting fragmented or something of that
sort. The alloc/free pair of declarations appear correct to me and I'm sure
that they work, although every single byte allocated when you create a new
block of a given size and later free it might not be freed, depending on how
the heap is managed.

If the goal is really to create a bunch of little allocations and then free
them all at once at the end, you might want to create a new heap and
allocate from that or manage the memory yourself.

Have you tried unmanaged code for comparison?

Paul T.
 
G

Giorgos

Now it's getting a bit confusing. The problem is that after using the
pocket pc for some time, available RAM decreases even though
applications are closed down. This memory gets released after a soft
reset.

I read about the memory management, (WM_Hibernate etc.) and my idea was
to force the system to release as much memory as possible, by taking up
all available memory, waiting for some time and then releasing it. It
really works but as I said earlier, the memory gets released after I
close my application. This way I cannot inform the user about how much
memory gets free after running my program. Also when I allocate the
memory in big amounts (1 mb instead of 1kb) the system does not
respond... ( I hope you can understand what I am saying).

I realize that what I am trying to do is probably 'immature'
programming, but I am really new to this. Please let me know if I am
doing things in the wrong way. I will start reading more about memory
management and maybe find the right way to do it ! Thanks for your
suggestions. The keywords in your reply will help me a lot.
 
G

Guest

You are most certainly doing it wrong. You need to find the source of the
leak and plug it. What you're doing is a recipe for poor performance and
absolutely no gain.

-Chris
 
P

Paul G. Tobey [eMVP]

Let's see. If you call LocalAlloc() from managed code, that allocates from
the local heap. If you grow that heap really, really big and then free some
objects, does any of the memory get released to the OS? Or just back into
the free part of the local heap?

Since you're allocating so much stuff, I'd try creating my own heap using
HeapCreate() and do the allocs and frees using HeapAlloc() and HeapFree(),
and, if I need to dump everything in a low-memory situation, free that heap
entirely with HeapDestroy().

Paul T.
 
G

Giorgos

I'd like to thank everyone, especially Paul for his continues support
and guidance and Daniel for the great article.

I realized that the program as is at the moment works on a dell axim
but often fails on a qtek. So I am reading on the topic and I hope to
come up with a better idea soon.

To be honest after using a pocket pc for a couple of years I don't
have any problems with memory management. The only thing is when an
application or a game needs an extreme amount of memory you are forced
to perform a soft reset and now with wm5 this can take some time.
 

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