Memory problem with byte[]

  • Thread starter Thread starter Arsa
  • Start date Start date
A

Arsa

Hi,

When i set a byte array to null the memory doesn't seem to be freed. Here is
sample code where the problem occurs:

private void testBytesArray()
{
byte[] BA1 = null;
try
{
BA1 = new byte[2000000];
for (int i = 0; i<2000; i++)
{
BA1 = (byte)i;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
BA1 = null;
}
}

The BA1 takes 2Mb of memory, after calling BA1 = null the memory used
remains 2Mb and when calling a second time BA1 = new byte[2000000] it jumps
to 4Mb. Is it a know issue in the compact framework with the GC or somthing
missing in my code? Any work around possible to release the memory?

Thanks,
Arsa
 
I don't know for certain, but I don't see any reason to believe that the
unreferenced memory for the array would be freed *immediately*. It seems
like it would be freed on the next garbage collection cycle.

Paul T.
 
You can try calling GC.Collect, but in general the memory will be freed when
the next allocation request cannot be satisfied, or the system becomes idle
 
Thanks for the reply.
I know the GC will not run Immediatly. But what is annoying is that I am
using 2 big bytes arrays in my function. Between two calls of that function
the memory is not always freed, even if I manually run the GC.Collect(). On
the next calls, the memory used is growing which somtimes causes an
"OutOfMemoryException".
By the way when I send my application background and bring it back (That
causes GC to run), memory is freed.
What is the right way to force the GC to run correctly? GC.Collect() alone
doesn't seem to clear memory.
Or is there a better work around to properly clear the memory used by bytes
array "byte[]" without using GC?

Thanks in advance,
Arsa


Alex Feinman said:
You can try calling GC.Collect, but in general the memory will be freed when
the next allocation request cannot be satisfied, or the system becomes idle

Arsa said:
Hi,

When i set a byte array to null the memory doesn't seem to be freed.
Here
is
sample code where the problem occurs:

private void testBytesArray()
{
byte[] BA1 = null;
try
{
BA1 = new byte[2000000];
for (int i = 0; i<2000; i++)
{
BA1 = (byte)i;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
BA1 = null;
}
}

The BA1 takes 2Mb of memory, after calling BA1 = null the memory used
remains 2Mb and when calling a second time BA1 = new byte[2000000] it jumps
to 4Mb. Is it a know issue in the compact framework with the GC or somthing
missing in my code? Any work around possible to release the memory?

Thanks,
Arsa

 
You should probably post a very simple example that fails with the Out Of
Memory exception and let us know what version of the framework you are
using. It sounds like a bug in the framework, just based on your
description.

Paul T.

Arsa said:
Thanks for the reply.
I know the GC will not run Immediatly. But what is annoying is that I am
using 2 big bytes arrays in my function. Between two calls of that function
the memory is not always freed, even if I manually run the GC.Collect(). On
the next calls, the memory used is growing which somtimes causes an
"OutOfMemoryException".
By the way when I send my application background and bring it back (That
causes GC to run), memory is freed.
What is the right way to force the GC to run correctly? GC.Collect() alone
doesn't seem to clear memory.
Or is there a better work around to properly clear the memory used by bytes
array "byte[]" without using GC?

Thanks in advance,
Arsa


Alex Feinman said:
You can try calling GC.Collect, but in general the memory will be freed when
the next allocation request cannot be satisfied, or the system becomes idle

Arsa said:
Hi,

When i set a byte array to null the memory doesn't seem to be freed.
Here
is
sample code where the problem occurs:

private void testBytesArray()
{
byte[] BA1 = null;
try
{
BA1 = new byte[2000000];
for (int i = 0; i<2000; i++)
{
BA1 = (byte)i;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
BA1 = null;
}
}

The BA1 takes 2Mb of memory, after calling BA1 = null the memory used
remains 2Mb and when calling a second time BA1 = new byte[2000000] it jumps
to 4Mb. Is it a know issue in the compact framework with the GC or somthing
missing in my code? Any work around possible to release the memory?

Thanks,
Arsa


 
How are you measuring the amount of memory being used? If it is by looking
at the Windows CE memory control panel, there is one thing you need to be
aware of. Windows CE can be "lazy" about freeing memory. If you allocate a
block of memory and then free it, CE can make the decision to not
completely release the memory until some later time of its choosing (to
batch up operations or to wait until it is necessary to release the memory
because resources are running low).

So to see if the .NETCF garbage collector is actually freeing your object,
it would be better to use System.GC.GetTotalMemory(false). That will report
the amount of memory currently under the control of .NETCF. I have run the
code you provided in your original post and at least on my system the first
array gets collected properly when a second array is created.

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Thanks you for your reply.
I'm as well using System.GC.GetTotalMemory to measure the memory. Cause if
you go to the panel (the application goes backgroud) and the memory is
released.
On my system, after the first call, one of the array is collected correctly,
but 4Mb are never released. So memory will go from 4 to 12 then back to 4
again. When the application goes background the 4Mb are collcted correctly.
I hope it's clear. Is it normal?
Thanks,
Arsa
 
Hi Paul and Group,
Here is a sample application in attachement.
It's a simple form with a button. First Click is ok but the second crashes
(I have small memory on a Dell Aximx5 32M, and CompactFramework SP2). I uses
a function called shouMem to desplay the memory using GC.ShowMemoryUse.
Please let me know how it behaves.
Am I doing something wrong? is there any work around?
Thanks in advance.
Arsa

Paul G. Tobey said:
You should probably post a very simple example that fails with the Out Of
Memory exception and let us know what version of the framework you are
using. It sounds like a bug in the framework, just based on your
description.

Paul T.

Arsa said:
Thanks for the reply.
I know the GC will not run Immediatly. But what is annoying is that I am
using 2 big bytes arrays in my function. Between two calls of that function
the memory is not always freed, even if I manually run the GC.Collect(). On
the next calls, the memory used is growing which somtimes causes an
"OutOfMemoryException".
By the way when I send my application background and bring it back (That
causes GC to run), memory is freed.
What is the right way to force the GC to run correctly? GC.Collect() alone
doesn't seem to clear memory.
Or is there a better work around to properly clear the memory used by bytes
array "byte[]" without using GC?

Thanks in advance,
Arsa


"Alex Feinman [MVP]" <[email protected]> a écrit dans le message
de news:[email protected]...
You can try calling GC.Collect, but in general the memory will be
freed
when
the next allocation request cannot be satisfied, or the system becomes idle


Hi,

When i set a byte array to null the memory doesn't seem to be freed. Here
is
sample code where the problem occurs:

private void testBytesArray()
{
byte[] BA1 = null;
try
{
BA1 = new byte[2000000];
for (int i = 0; i<2000; i++)
{
BA1 = (byte)i;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
BA1 = null;
}
}

The BA1 takes 2Mb of memory, after calling BA1 = null the memory used
remains 2Mb and when calling a second time BA1 = new byte[2000000] it
jumps
to 4Mb. Is it a know issue in the compact framework with the GC or
somthing
missing in my code? Any work around possible to release the memory?

Thanks,
Arsa


 
Sorry It's GC.GetTotalMemory(flase) (not GC.ShowMemoryUse)

Arsa said:
Hi Paul and Group,
Here is a sample application in attachement.
It's a simple form with a button. First Click is ok but the second crashes
(I have small memory on a Dell Aximx5 32M, and CompactFramework SP2). I uses
a function called shouMem to desplay the memory using GC.ShowMemoryUse.
Please let me know how it behaves.
Am I doing something wrong? is there any work around?
Thanks in advance.
Arsa

"Paul G. Tobey [eMVP]" <ptobey_no_spam@instrument_no_spam.com> a écrit dans
le message de news:[email protected]...
You should probably post a very simple example that fails with the Out Of
Memory exception and let us know what version of the framework you are
using. It sounds like a bug in the framework, just based on your
description.

Paul T.

Arsa said:
Thanks for the reply.
I know the GC will not run Immediatly. But what is annoying is that I am
using 2 big bytes arrays in my function. Between two calls of that function
the memory is not always freed, even if I manually run the
GC.Collect().
On
the next calls, the memory used is growing which somtimes causes an
"OutOfMemoryException".
By the way when I send my application background and bring it back (That
causes GC to run), memory is freed.
What is the right way to force the GC to run correctly? GC.Collect() alone
doesn't seem to clear memory.
Or is there a better work around to properly clear the memory used by bytes
array "byte[]" without using GC?

Thanks in advance,
Arsa


"Alex Feinman [MVP]" <[email protected]> a écrit dans le message
de You can try calling GC.Collect, but in general the memory will be freed
when
the next allocation request cannot be satisfied, or the system becomes
idle


Hi,

When i set a byte array to null the memory doesn't seem to be freed.
Here
is
sample code where the problem occurs:

private void testBytesArray()
{
byte[] BA1 = null;
try
{
BA1 = new byte[2000000];
for (int i = 0; i<2000; i++)
{
BA1 = (byte)i;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
BA1 = null;
}
}

The BA1 takes 2Mb of memory, after calling BA1 = null the memory used
remains 2Mb and when calling a second time BA1 = new byte[2000000] it
jumps
to 4Mb. Is it a know issue in the compact framework with the GC or
somthing
missing in my code? Any work around possible to release the memory?

Thanks,
Arsa


 
Regrettably you have discovered a bug in all versions of .NETCF for
ARM-compatible devices. We are still investigating the problem and
potential solutions. This bug can cause a small number of memory objects to
remain allocated even when they are no longer referenced. In our testing,
no more than 6 objects can be "leaked" in this way and typically the number
is much smaller. Since only a limited number of objects can be affected, in
most cases the impact is not noticeable, but because the objects allocated
by your application are so large, leaking even a few of them is more
serious.

Since this bug only exists on ARM-compatible devices, it is possible to
confirm that you are experiencing this particular problem by running your
application in the PocketPC emulator supplied with Visual Studio. If memory
is leaked on your device but not the emulator, then it is possible you are
experiencing this problem.

In more technical detail, the bug is caused by .NETCF erroneously believing
that an uninitialized block of memory outside of your application’s control
may contain object references. If by chance the data within this
uninitialized block points to a valid object, that object (and any objects
it references) will be kept alive indefinitely. The larger an object is,
the greater the likelihood that one of these semi-random uninitialized
values will fall within the bounds of the object. Therefore a very large
object (such as the ones your application is allocating) is more likely to
be leaked than a small object.

At this time we know of no way to completely avoid this problem or predict
exactly which objects will be leaked. In your particular case, the impact
of the bug could be lessened by allocating smaller objects -- the same
number of objects will likely be leaked but the total amount of memory
consumed will be less.

We are working on a solution and we will notify you when it is released.

Thank you for the report.

--------------------
From: "Arsa" <[email protected]>
Subject: Re: Memory problem with byte[]
Date: Fri, 16 Jan 2004 21:04:41 +0100

Hi Paul and Group,
Here is a sample application in attachement.
It's a simple form with a button. First Click is ok but the second crashes
(I have small memory on a Dell Aximx5 32M, and CompactFramework SP2). I uses
a function called shouMem to desplay the memory using GC.ShowMemoryUse.
Please let me know how it behaves.
Am I doing something wrong? is there any work around?
Thanks in advance.
Arsa

Paul G. Tobey said:
You should probably post a very simple example that fails with the Out Of
Memory exception and let us know what version of the framework you are
using. It sounds like a bug in the framework, just based on your
description.

Paul T.

Arsa said:
Thanks for the reply.
I know the GC will not run Immediatly. But what is annoying is that I am
using 2 big bytes arrays in my function. Between two calls of that function
the memory is not always freed, even if I manually run the
GC.Collect().
On
the next calls, the memory used is growing which somtimes causes an
"OutOfMemoryException".
By the way when I send my application background and bring it back (That
causes GC to run), memory is freed.
What is the right way to force the GC to run correctly? GC.Collect() alone
doesn't seem to clear memory.
Or is there a better work around to properly clear the memory used by bytes
array "byte[]" without using GC?

Thanks in advance,
Arsa


"Alex Feinman [MVP]" <[email protected]> a écrit dans le message
de You can try calling GC.Collect, but in general the memory will be freed
when
the next allocation request cannot be satisfied, or the system becomes
idle


Hi,

When i set a byte array to null the memory doesn't seem to be freed.
Here
is
sample code where the problem occurs:

private void testBytesArray()
{
byte[] BA1 = null;
try
{
BA1 = new byte[2000000];
for (int i = 0; i<2000; i++)
{
BA1 = (byte)i;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
BA1 = null;
}
}

The BA1 takes 2Mb of memory, after calling BA1 = null the memory used
remains 2Mb and when calling a second time BA1 = new byte[2000000] it
jumps
to 4Mb. Is it a know issue in the compact framework with the GC or
somthing
missing in my code? Any work around possible to release the memory?

Thanks,
Arsa




This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Brian,

Thank you for your detailed and clear reply. This is exaclty what happens.
I hope you will get a solution for that soon.
Thanks,

Arsa


Brian Smith said:
Regrettably you have discovered a bug in all versions of .NETCF for
ARM-compatible devices. We are still investigating the problem and
potential solutions. This bug can cause a small number of memory objects to
remain allocated even when they are no longer referenced. In our testing,
no more than 6 objects can be "leaked" in this way and typically the number
is much smaller. Since only a limited number of objects can be affected, in
most cases the impact is not noticeable, but because the objects allocated
by your application are so large, leaking even a few of them is more
serious.

Since this bug only exists on ARM-compatible devices, it is possible to
confirm that you are experiencing this particular problem by running your
application in the PocketPC emulator supplied with Visual Studio. If memory
is leaked on your device but not the emulator, then it is possible you are
experiencing this problem.

In more technical detail, the bug is caused by .NETCF erroneously believing
that an uninitialized block of memory outside of your application's control
may contain object references. If by chance the data within this
uninitialized block points to a valid object, that object (and any objects
it references) will be kept alive indefinitely. The larger an object is,
the greater the likelihood that one of these semi-random uninitialized
values will fall within the bounds of the object. Therefore a very large
object (such as the ones your application is allocating) is more likely to
be leaked than a small object.

At this time we know of no way to completely avoid this problem or predict
exactly which objects will be leaked. In your particular case, the impact
of the bug could be lessened by allocating smaller objects -- the same
number of objects will likely be leaked but the total amount of memory
consumed will be less.

We are working on a solution and we will notify you when it is released.

Thank you for the report.

--------------------
From: "Arsa" <[email protected]>
Subject: Re: Memory problem with byte[]
Date: Fri, 16 Jan 2004 21:04:41 +0100

Hi Paul and Group,
Here is a sample application in attachement.
It's a simple form with a button. First Click is ok but the second crashes
(I have small memory on a Dell Aximx5 32M, and CompactFramework SP2). I uses
a function called shouMem to desplay the memory using GC.ShowMemoryUse.
Please let me know how it behaves.
Am I doing something wrong? is there any work around?
Thanks in advance.
Arsa

"Paul G. Tobey [eMVP]" <ptobey_no_spam@instrument_no_spam.com> a écrit dans
le message de news:[email protected]...
You should probably post a very simple example that fails with the Out Of
Memory exception and let us know what version of the framework you are
using. It sounds like a bug in the framework, just based on your
description.

Paul T.

Thanks for the reply.
I know the GC will not run Immediatly. But what is annoying is that I am
using 2 big bytes arrays in my function. Between two calls of that
function
the memory is not always freed, even if I manually run the GC.Collect().
On
the next calls, the memory used is growing which somtimes causes an
"OutOfMemoryException".
By the way when I send my application background and bring it back (That
causes GC to run), memory is freed.
What is the right way to force the GC to run correctly? GC.Collect() alone
doesn't seem to clear memory.
Or is there a better work around to properly clear the memory used by
bytes
array "byte[]" without using GC?

Thanks in advance,
Arsa


"Alex Feinman [MVP]" <[email protected]> a écrit dans le message
de You can try calling GC.Collect, but in general the memory will be freed
when
the next allocation request cannot be satisfied, or the system becomes
idle


Hi,

When i set a byte array to null the memory doesn't seem to be freed.
Here
is
sample code where the problem occurs:

private void testBytesArray()
{
byte[] BA1 = null;
try
{
BA1 = new byte[2000000];
for (int i = 0; i<2000; i++)
{
BA1 = (byte)i;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
BA1 = null;
}
}

The BA1 takes 2Mb of memory, after calling BA1 = null the memory used
remains 2Mb and when calling a second time BA1 = new
byte[2000000]
it
jumps
to 4Mb. Is it a know issue in the compact framework with the GC or
somthing
missing in my code? Any work around possible to release the memory?

Thanks,
Arsa


This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top