473,385 Members | 2,069 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,385 software developers and data experts.

Memory Usage

I was doing starting some experimenting with the GC and ran into the following odd result. I created an object and my used memory went up by almost 11k but when I cleared it and forced a collection the used memory only went down by 12 bytes.

Any ideas on what else is being created or not cleaned up?

Code ==>

Sub Main()
' The following VB.NET code shows how memory is allocated and deallocated during object creation and destruction.

Console.WriteLine("Memory used before object creation : " & GC.GetTotalMemory(True))

Dim obj1 As New Object
Console.WriteLine("Memory used after object 1 creation : " & GC.GetTotalMemory(True))

Dim obj2 As New Object
Console.WriteLine("Memory used after object 2 creation : " & GC.GetTotalMemory(True))

obj1 = Nothing
GC.Collect()
Console.WriteLine("Memory used after cleanup : " & GC.GetTotalMemory(True))

Console.ReadLine()
End Sub

Output ==>

Memory used before object creation : 8168
Memory used after object 1 creation : 19100
Memory used after object 2 creation : 19112
Memory used after cleanup : 19100

You can see from the increase between object 1 and 2 that a new variable of type Object only costs 12 bytes. You also see that the value drops by 12 when the object is destroyed and garbage collected.
Jul 21 '05 #1
5 2214
Jason Callas <Ja*******@hotmail.com> wrote:
I was doing starting some experimenting with the GC and ran into the
following odd result. I created an object and my used memory went up
by almost 11k but when I cleared it and forced a collection the used
memory only went down by 12 bytes.

Any ideas on what else is being created or not cleaned up?


I suspect the first call to Console.WriteLine is causing a load of
extra memory to be allocated. Try putting:

Console.WriteLine ("Startup")

before the current first Console.WriteLine and see if that makes a
difference.

Note that the object obj1 refers to is actually eligible for GC
immediately in release mode, as it's never read.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
Hi, Jason

you won't get any sensible results using this sample IMO. Problem is that
apart from GC you might get some influence from JIT during runs. Depends on
settings and mode of compilation and execution. As Jon pointed out optimizer
can remove even some code from execution path.
Additionally, you might found out that console methods allocate more memory
than Object and use more resources. And last one, GetTotalMemory is
providing you with figure, which is "A number that is the best available
approximation of the number of bytes currently allocated in managed memory"
as MSDN says. Even if you force garbage collection GC is not guaranteed to
collect all inaccessible memory.

If you want to get real data and understanding, not approximations, you must
use profiler and performance counters for relevant objects.

HTH
Alex

"Jason Callas" <Ja*******@hotmail.com> wrote in message
news:e5**************@tk2msftngp13.phx.gbl...
I was doing starting some experimenting with the GC and ran into the
following odd result. I created an object and my used memory went up by
almost 11k but when I cleared it and forced a collection the used memory
only went down by 12 bytes.

Any ideas on what else is being created or not cleaned up?

Code ==>

Sub Main()
' The following VB.NET code shows how memory is allocated and
deallocated during object creation and destruction.

Console.WriteLine("Memory used before object creation : " &
GC.GetTotalMemory(True))

Dim obj1 As New Object
Console.WriteLine("Memory used after object 1 creation : " &
GC.GetTotalMemory(True))

Dim obj2 As New Object
Console.WriteLine("Memory used after object 2 creation : " &
GC.GetTotalMemory(True))

obj1 = Nothing
GC.Collect()
Console.WriteLine("Memory used after cleanup : " &
GC.GetTotalMemory(True))

Console.ReadLine()
End Sub

Output ==>

Memory used before object creation : 8168
Memory used after object 1 creation : 19100
Memory used after object 2 creation : 19112
Memory used after cleanup : 19100

You can see from the increase between object 1 and 2 that a new variable of
type Object only costs 12 bytes. You also see that the value drops by 12
when the object is destroyed and garbage collected.
Jul 21 '05 #3
Thanks for replying.

After the post I did some more testing. After mixing and matching
Console.WriteLine statements and creating new variables and stuff it seems
that it is the call the GC.GetTotalMemory that actually is causing the large
jump. (I did find that Console.WriteLine seems to use up about 4k
sometimes.) Not sure what the memory method is doing but it must be
creating stuff internally. No biggie.

I ended up putting the following code

Console.WriteLine(">> Ignore this line = " & GC.GetTotalMemory(True) & " <<"
& vbCrLf)

with brief comments at the beginning of my sample code. (I am writing a
whitepaper (with sample code) for internal company use.)

Thanks.

- Jason

"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> wrote in message
news:Ov**************@TK2MSFTNGP12.phx.gbl...
Hi, Jason

you won't get any sensible results using this sample IMO. Problem is that
apart from GC you might get some influence from JIT during runs. Depends on settings and mode of compilation and execution. As Jon pointed out optimizer can remove even some code from execution path.
Additionally, you might found out that console methods allocate more memory than Object and use more resources. And last one, GetTotalMemory is
providing you with figure, which is "A number that is the best available
approximation of the number of bytes currently allocated in managed memory" as MSDN says. Even if you force garbage collection GC is not guaranteed to
collect all inaccessible memory.

If you want to get real data and understanding, not approximations, you must use profiler and performance counters for relevant objects.

HTH
Alex

"Jason Callas" <Ja*******@hotmail.com> wrote in message
news:e5**************@tk2msftngp13.phx.gbl...
I was doing starting some experimenting with the GC and ran into the
following odd result. I created an object and my used memory went up by
almost 11k but when I cleared it and forced a collection the used memory
only went down by 12 bytes.

Any ideas on what else is being created or not cleaned up?

Code ==>

Sub Main()
' The following VB.NET code shows how memory is allocated and
deallocated during object creation and destruction.

Console.WriteLine("Memory used before object creation : " &
GC.GetTotalMemory(True))

Dim obj1 As New Object
Console.WriteLine("Memory used after object 1 creation : " &
GC.GetTotalMemory(True))

Dim obj2 As New Object
Console.WriteLine("Memory used after object 2 creation : " &
GC.GetTotalMemory(True))

obj1 = Nothing
GC.Collect()
Console.WriteLine("Memory used after cleanup : " &
GC.GetTotalMemory(True))

Console.ReadLine()
End Sub

Output ==>

Memory used before object creation : 8168
Memory used after object 1 creation : 19100
Memory used after object 2 creation : 19112
Memory used after cleanup : 19100

You can see from the increase between object 1 and 2 that a new variable of type Object only costs 12 bytes. You also see that the value drops by 12
when the object is destroyed and garbage collected.

Jul 21 '05 #4
Jason,

that's why I recommend to use profiler and not such benchmarks. I missed the
elephant, which would be very prominent in profiler statistics on heap and
calls - GetTotalMemory :-)

HTH
Alex

"Jason Callas" <Ja*******@hotmail.com> wrote in message
news:u2**************@TK2MSFTNGP09.phx.gbl...
Thanks for replying.

After the post I did some more testing. After mixing and matching
Console.WriteLine statements and creating new variables and stuff it seems
that it is the call the GC.GetTotalMemory that actually is causing the large jump. (I did find that Console.WriteLine seems to use up about 4k
sometimes.) Not sure what the memory method is doing but it must be
creating stuff internally. No biggie.

I ended up putting the following code

Console.WriteLine(">> Ignore this line = " & GC.GetTotalMemory(True) & " <<" & vbCrLf)

with brief comments at the beginning of my sample code. (I am writing a
whitepaper (with sample code) for internal company use.)

Thanks.

- Jason

"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> wrote in message
news:Ov**************@TK2MSFTNGP12.phx.gbl...
Hi, Jason

you won't get any sensible results using this sample IMO. Problem is that apart from GC you might get some influence from JIT during runs. Depends

on
settings and mode of compilation and execution. As Jon pointed out

optimizer
can remove even some code from execution path.
Additionally, you might found out that console methods allocate more

memory
than Object and use more resources. And last one, GetTotalMemory is
providing you with figure, which is "A number that is the best available
approximation of the number of bytes currently allocated in managed

memory"
as MSDN says. Even if you force garbage collection GC is not guaranteed to collect all inaccessible memory.

If you want to get real data and understanding, not approximations, you

must
use profiler and performance counters for relevant objects.

HTH
Alex

"Jason Callas" <Ja*******@hotmail.com> wrote in message
news:e5**************@tk2msftngp13.phx.gbl...
I was doing starting some experimenting with the GC and ran into the
following odd result. I created an object and my used memory went up by
almost 11k but when I cleared it and forced a collection the used memory
only went down by 12 bytes.

Any ideas on what else is being created or not cleaned up?

Code ==>

Sub Main()
' The following VB.NET code shows how memory is allocated and
deallocated during object creation and destruction.

Console.WriteLine("Memory used before object creation : " &
GC.GetTotalMemory(True))

Dim obj1 As New Object
Console.WriteLine("Memory used after object 1 creation : " &
GC.GetTotalMemory(True))

Dim obj2 As New Object
Console.WriteLine("Memory used after object 2 creation : " &
GC.GetTotalMemory(True))

obj1 = Nothing
GC.Collect()
Console.WriteLine("Memory used after cleanup : " &
GC.GetTotalMemory(True))

Console.ReadLine()
End Sub

Output ==>

Memory used before object creation : 8168
Memory used after object 1 creation : 19100
Memory used after object 2 creation : 19112
Memory used after cleanup : 19100

You can see from the increase between object 1 and 2 that a new variable

of
type Object only costs 12 bytes. You also see that the value drops by 12
when the object is destroyed and garbage collected.


Jul 21 '05 #5
How about allocating a million of objects instead. This way the error of overhead may be smaller and you can get more accurate result.
"Jason Callas" <Ja*******@hotmail.com> wrote in message news:e5**************@tk2msftngp13.phx.gbl...
I was doing starting some experimenting with the GC and ran into the following odd result. I created an object and my used memory went up by almost 11k but when I cleared it and forced a collection the used memory only went down by 12 bytes.

Any ideas on what else is being created or not cleaned up?

Code ==>

Sub Main()
' The following VB.NET code shows how memory is allocated and deallocated during object creation and destruction.

Console.WriteLine("Memory used before object creation : " & GC.GetTotalMemory(True))

Dim obj1 As New Object
Console.WriteLine("Memory used after object 1 creation : " & GC.GetTotalMemory(True))

Dim obj2 As New Object
Console.WriteLine("Memory used after object 2 creation : " & GC.GetTotalMemory(True))

obj1 = Nothing
GC.Collect()
Console.WriteLine("Memory used after cleanup : " & GC.GetTotalMemory(True))

Console.ReadLine()
End Sub

Output ==>

Memory used before object creation : 8168
Memory used after object 1 creation : 19100
Memory used after object 2 creation : 19112
Memory used after cleanup : 19100

You can see from the increase between object 1 and 2 that a new variable of type Object only costs 12 bytes. You also see that the value drops by 12 when the object is destroyed and garbage collected.
Jul 21 '05 #6

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

Similar topics

8
by: rbt | last post by:
Would a Python process consume more memory on a PC with lots of memory? For example, say I have the same Python script running on two WinXP computers that both have Python 2.4.0. One computer has...
5
by: Justice | last post by:
Currently I'm doing some experimenting with the XMLHTTP object in Javascript. Now, the XMLHttp object is asynchronous (at least in this case), and the following code causes a significant memory...
2
by: tomvr | last post by:
Hello I have noticed some 'weird' memory usage in a vb.net windows app The situation is as follows I have an app (heavy on images) with 2 forms (actually there are more forms and on starting...
6
by: Tom | last post by:
We have a VERY simple .NET C# Form Application, that has about a 23MB Memory Footprint. It starts a window runs a process and does a regular expression. I have done a GC.Collect to make sure that,...
2
by: Jarvis | last post by:
I've made a testing program to test the memory usage of some Data Forms. I create a MDI parent form with one single MDI child form, which is a Data Form generated by .NET Data Form Wizard. To...
3
by: Ian Taite | last post by:
Hello, I'm exploring why one of my C# .NET apps has "high" memory usage, and whether I can reduce the memory usage. I have an app that wakes up and processes text files into a database...
20
by: Philip Carnstam | last post by:
How come .Net applications use so much memory? Every application I compile uses at least 10 MB of memory, even the ones consisting of only a form and nothing else. If I minimize them though the...
13
by: Ilias Lazaridis | last post by:
How to detect memory leaks of python programms, which run in an environment like this: * Suse Linux 9.3 * Apache * mod_python The problem occoured after some updates on the infrastructure....
1
by: yzghan | last post by:
Hi all, I feel that my python script is leaking memory. And this is a test I have: log.write(" " + "test() ... memory usage: " + " ".join(repr(i/(1024*1024)) for i in getMemInfo()) + "\n") m...
1
by: Jean-Paul Calderone | last post by:
On Tue, 22 Apr 2008 14:54:37 -0700 (PDT), yzghan@gmail.com wrote: The test doesn't demonstrate any leaks. It does demonstrate that memory usage can remain at or near peak memory usage even after...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.