473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

CreateMeasurementGraphics memory leak

Hi.

Could someone please tell me how I should correctly dispose of the
CreateMeasurementGraphics reference to stop the memory leak in the
following code:
For x = 1 To 10000
Dim PrintDocument As New Printing.PrintDocument
Dim g As Graphics =
PrintDocument.PrinterSettings.CreateMeasurementGra phics
Dim hdc As IntPtr = g.GetHdc()
g.ReleaseHdc(hdc)
g.Dispose()
PrintDocument.Dispose()
Next

Your help in much appreciated.

Dave
Nov 20 '05 #1
11 2312
Hi Dave,

Are you sure it is a memoryleak, the dispose does not free memory, it set
the object in a state that the the Garbage Collector knows that it can set
free.

http://support.microsoft.com/default...b;en-us;317866

Did you have a look at that?

Cor
Nov 20 '05 #2
Hi Dave,

Are you sure it is a memoryleak, the dispose does not free memory, it set
the object in a state that the the Garbage Collector knows that it can set
free.

http://support.microsoft.com/default...b;en-us;317866

Did you have a look at that?

Cor
Nov 20 '05 #3
"Cor Ligthert" <no**********@planet.nl> schrieb
Hi Dave,

Are you sure it is a memoryleak, the dispose does not free memory, it
set the object in a state that the the Garbage Collector knows that
it can set free.

http://support.microsoft.com/default...b;en-us;317866

Did you have a look at that?


I did not follow the link, but what you wrote is wrong. Dispose can free
memory if it's one of the types of unmanaged resources the object allocated.
Dispose also does not free the object to be available for the GC. Dispose
has Nothing to do with GC. At least as long as there's still a reference on
the object, the object won't be collected, even if you called Dispose.
--
Armin

Nov 20 '05 #4
Hi Armin,

I did not follow the link, but what you wrote is wrong. Dispose can free
memory if it's one of the types of unmanaged resources the object allocated. Dispose also does not free the object to be available for the GC. Dispose
has Nothing to do with GC. At least as long as there's still a reference on the object, the object won't be collected, even if you called Dispose.

Agree, however look at the problem.

There are created 10000 new graphic objects in a single routine which stays
in scoop.

In my idea those objects will not be removed until the routine is ended.

My idea was that maybe force the GC to start can help.

However, never tried it.
And maybe you have a better idea.

Or what I am thinking on writting this, maybe it is just easier to create a
method to create the object, than it will go everytime out of scoop.

Cor

Nov 20 '05 #5
"Cor Ligthert" <no**********@planet.nl> schrieb
Hi Armin,

I did not follow the link, but what you wrote is wrong. Dispose can
free memory if it's one of the types of unmanaged resources the
object allocated.
Dispose also does not free the object to be available for the GC.
Dispose has Nothing to do with GC. At least as long as there's
still a reference

on
the object, the object won't be collected, even if you called
Dispose.

Agree, however look at the problem.

There are created 10000 new graphic objects in a single routine which
stays in scoop.

In my idea those objects will not be removed until the routine is
ended.


g stores *one* reference only.
My idea was that maybe force the GC to start can help.

However, never tried it.
And maybe you have a better idea.

Or what I am thinking on writting this, maybe it is just easier to
create a method to create the object, than it will go everytime out
of scoop.


Yes and no. Going out of scope of course removes the reference because the
variable is destroyed, but in the OP's code, the reference is also removed
because it is overwritten by the next reference. It wouldn't make a big
difference. So, after the loop, before the procedure is left, there is 1
referenced and not collectable object, and 9999 not referenced and
collectable objects. As you know GC.collect (and optionally
gc.waitforpendingfinalizers) could destroy the objects immediatelly and
release memory, but you probably also know that this actually shouldn't be
done because we should rely on the GC and have it do the work when it
"thinks" it's the time.
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #6
* de************@yahoo.com.au (Dave) scripsit:
Could someone please tell me how I should correctly dispose of the
CreateMeasurementGraphics reference to stop the memory leak in the
following code:
For x = 1 To 10000
Dim PrintDocument As New Printing.PrintDocument
Dim g As Graphics =
PrintDocument.PrinterSettings.CreateMeasurementGra phics
Dim hdc As IntPtr = g.GetHdc()
g.ReleaseHdc(hdc)
g.Dispose()
PrintDocument.Dispose()
Next


How did you find out that there is a memory leak? Did you have a lookat
the GDI handles in Task Manager?

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #7
hi***************@gmx.at (Herfried K. Wagner [MVP]) wrote in message

How did you find out that there is a memory leak? Did you have a lookat
the GDI handles in Task Manager?


Thanks everyone for your input. I was looking at the mem usage in the
task manager. I suppose Memory leak may not be the correct term for
the problem here, however I couldn't think of a better term do descibe
it as the garbage collector never seems to release the memory, even if
I try to force it to.

If you run the sample code you will quickly see the problem. My
application acts as a print spooler service and memusage eventually
gets to a point where the server blue screens.
Nov 20 '05 #8
"Dave" <de************@yahoo.com.au> schrieb
hi***************@gmx.at (Herfried K. Wagner [MVP]) wrote in message

How did you find out that there is a memory leak? Did you have a
lookat the GDI handles in Task Manager?


Thanks everyone for your input. I was looking at the mem usage in
the task manager. I suppose Memory leak may not be the correct term
for the problem here, however I couldn't think of a better term do
descibe it as the garbage collector never seems to release the
memory, even if I try to force it to.

If you run the sample code you will quickly see the problem. My
application acts as a print spooler service and memusage
eventually gets to a point where the server blue screens.

Yes, looks like there should be a Dispose method in the Printdocument class
(one that is not inherited). Means I can repro the problem. GDI handles are
reserved but not released (according to taskman), despite calling dispose
and forcing garbage collection.
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #9
"Armin Zingler" <az*******@freenet.de> wrote in message

Yes, looks like there should be a Dispose method in the Printdocument class
(one that is not inherited). Means I can repro the problem. GDI handles are
reserved but not released (according to taskman), despite calling dispose
and forcing garbage collection.


I was hoping my ineptitude was the cause of the problem. Oh well.
Should I report this issue to MS or will one of the MVP's follow it
up?
Nov 20 '05 #10
For anyone reading this; as a workaround i've set up a timer to check
that the System.Environment.WorkingSet is under a reasonable amount.
If it's over, the application starts a new instance of itself then
closes. Dodgy but effective.
Regards,

Dave
Nov 20 '05 #11
* de************@yahoo.com.au (Dave) scripsit:
For anyone reading this; as a workaround i've set up a timer to check
that the System.Environment.WorkingSet is under a reasonable amount.
If it's over, the application starts a new instance of itself then
closes. Dodgy but effective.


LOL!

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? ...
17
by: José Joye | last post by:
Hi, I have implemented a Service that is responsible for getting messages from a MS MQ located on a remote machine. I'm getting memory leak from time to time (???). In some situation, it is...
4
by: Don Nell | last post by:
Hello Why is there a memory leak when this code is executed. for(;;) { ManagementScope scope = new ManagementScope(); scope.Options.Username="username"; scope.Options.Password="password";...
20
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex...
0
by: Dave | last post by:
Hi. Could someone please tell me how I should correctly dispose of the CreateMeasurementGraphics reference to stop the memory leak in the following code: For x = 1 To 10000 Dim...
23
by: James | last post by:
The following code will create memory leaks!!! using System; using System.Diagnostics; using System.Data; using System.Data.SqlClient; namespace MemoryLeak
8
by: Adrian | last post by:
Hi I have a JS program that runs localy (under IE6 only) on a PC but it has a memory leak (probably the known MS one!) What applications are there that I could use to look at the memory usage of...
3
by: Jim Land | last post by:
Jack Slocum claims here http://www.jackslocum.com/yui/2006/10/02/3-easy-steps-to-avoid-javascript- memory-leaks/ that "almost every site you visit that uses JavaScript is leaking memory". ...
22
by: Peter | last post by:
I am using VS2008. I have a Windows Service application which creates Crystal Reports. This is a multi theaded application which can run several reports at one time. My problem - there is a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.