Virtual Memory

G

Guest

I have created the followinga simple Managed C++ windows application with a
single button. Here is my button_onclick code



private: System::Void button1_Click(System::Object * sender,
System::EventArgs * e)
{
FileStream *myFile;
try
{
myFile = new FileStream(
S"A VERY LARGE 300 MEGABYTE FILE",
FileMode::Open,
FileAccess::Read);
}
catch (Exception *e)
{
return;
}

if (myFile != NULL)
{
__int64 myFileSize = myFile->get_Length();

Byte myFileData[] = new Byte[(int) myFileSize];
myFile->Read(myFileData, 0, (int) myFileSize);

delete myFileData;
}
}

I observe the following behavior via the Windows Task Manager

When I create the ByteArray, allocated Virtual Memory remains the same
In response to the Read statement, allocated Virtual Memory increases by the
size of the file and never decreases.

If I repeatedly click the button in my form, the virtual memory size
fuctuates. Sometimes it 1x the file size, sometimes it is 2x the file size,
or 3x the file size. Eventually, if I click the button often enough, the
application dies with an OutOfMemory exception when it exceeds 1.4 gB.

How can I force the framework to minimize the amount of Virtual Memory being
used?
 
R

RBischoff

Hello (e-mail address removed),
Remember, FileStream need to be disposed.

Add a finally block, as so:
__finally {
if (myFile) __try_cast<IDisposable*>(myFile)->Dispose();
}

Best of luck!

Your C# ally ,
RBischoff


h> I have created the followinga simple Managed C++ windows application
h> with a single button. Here is my button_onclick code
h>
h> private: System::Void button1_Click(System::Object * sender,
h> System::EventArgs * e)
h> {
h> FileStream *myFile;
h> try
h> {
h> myFile = new FileStream(
h> S"A VERY LARGE 300 MEGABYTE FILE",
h> FileMode::Open,
h> FileAccess::Read);
h> }
h> catch (Exception *e)
h> {
h> return;
h> }
h> if (myFile != NULL)
h> {
h> __int64 myFileSize = myFile->get_Length();
h> Byte myFileData[] = new Byte[(int) myFileSize];
h> myFile->Read(myFileData, 0, (int) myFileSize);
h> delete myFileData;
h> }
h> }
h> I observe the following behavior via the Windows Task Manager
h>
h> When I create the ByteArray, allocated Virtual Memory remains the
h> same
h> In response to the Read statement, allocated Virtual Memory increases
h> by the
h> size of the file and never decreases.
h> If I repeatedly click the button in my form, the virtual memory size
h> fuctuates. Sometimes it 1x the file size, sometimes it is 2x the
h> file size, or 3x the file size. Eventually, if I click the button
h> often enough, the application dies with an OutOfMemory exception
h> when it exceeds 1.4 gB.
h>
h> How can I force the framework to minimize the amount of Virtual
h> Memory being used?
h>
 
G

Guest

Adding a call to myFile->Dispose did not fix the problem. The C++ environment
should be generating this call when the FileStream object goes out of scope.

Adding a GC::Collect() call at the end of the button_onclick seems to limit
the damage in that the virtual memory jumps around in a range 1x to 2x the
file size and the program no longer crashes.

Opening a second zero length file and then invoking GC::Collect is
sufficient to reset both the physical and virtuak memory. My hypothesis is
that the Framework is creating a FileMap under the covers, which explains the
increase in the virtual memory size by the size of the file and an increase
in the physical memory size proportional to the size of the buffer which is
being read.
 

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