473,657 Members | 2,513 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.WriteLi ne("Memory used before object creation : " & GC.GetTotalMemo ry(True))

Dim obj1 As New Object
Console.WriteLi ne("Memory used after object 1 creation : " & GC.GetTotalMemo ry(True))

Dim obj2 As New Object
Console.WriteLi ne("Memory used after object 2 creation : " & GC.GetTotalMemo ry(True))

obj1 = Nothing
GC.Collect()
Console.WriteLi ne("Memory used after cleanup : " & GC.GetTotalMemo ry(True))

Console.ReadLin e()
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 2250
Jason Callas <Ja*******@hotm ail.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.WriteLi ne is causing a load of
extra memory to be allocated. Try putting:

Console.WriteLi ne ("Startup")

before the current first Console.WriteLi ne 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.co m>
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*******@hotm ail.com> wrote in message
news:e5******** ******@tk2msftn gp13.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.WriteLi ne("Memory used before object creation : " &
GC.GetTotalMemo ry(True))

Dim obj1 As New Object
Console.WriteLi ne("Memory used after object 1 creation : " &
GC.GetTotalMemo ry(True))

Dim obj2 As New Object
Console.WriteLi ne("Memory used after object 2 creation : " &
GC.GetTotalMemo ry(True))

obj1 = Nothing
GC.Collect()
Console.WriteLi ne("Memory used after cleanup : " &
GC.GetTotalMemo ry(True))

Console.ReadLin e()
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.WriteLi ne statements and creating new variables and stuff it seems
that it is the call the GC.GetTotalMemo ry that actually is causing the large
jump. (I did find that Console.WriteLi ne 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.WriteLi ne(">> Ignore this line = " & GC.GetTotalMemo ry(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***********@ SPAMsympaticoPL EASE.ca> wrote in message
news:Ov******** ******@TK2MSFTN GP12.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*******@hotm ail.com> wrote in message
news:e5******** ******@tk2msftn gp13.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.WriteLi ne("Memory used before object creation : " &
GC.GetTotalMemo ry(True))

Dim obj1 As New Object
Console.WriteLi ne("Memory used after object 1 creation : " &
GC.GetTotalMemo ry(True))

Dim obj2 As New Object
Console.WriteLi ne("Memory used after object 2 creation : " &
GC.GetTotalMemo ry(True))

obj1 = Nothing
GC.Collect()
Console.WriteLi ne("Memory used after cleanup : " &
GC.GetTotalMemo ry(True))

Console.ReadLin e()
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*******@hotm ail.com> wrote in message
news:u2******** ******@TK2MSFTN GP09.phx.gbl...
Thanks for replying.

After the post I did some more testing. After mixing and matching
Console.WriteLi ne statements and creating new variables and stuff it seems
that it is the call the GC.GetTotalMemo ry that actually is causing the large jump. (I did find that Console.WriteLi ne 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.WriteLi ne(">> Ignore this line = " & GC.GetTotalMemo ry(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***********@ SPAMsympaticoPL EASE.ca> wrote in message
news:Ov******** ******@TK2MSFTN GP12.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*******@hotm ail.com> wrote in message
news:e5******** ******@tk2msftn gp13.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.WriteLi ne("Memory used before object creation : " &
GC.GetTotalMemo ry(True))

Dim obj1 As New Object
Console.WriteLi ne("Memory used after object 1 creation : " &
GC.GetTotalMemo ry(True))

Dim obj2 As New Object
Console.WriteLi ne("Memory used after object 2 creation : " &
GC.GetTotalMemo ry(True))

obj1 = Nothing
GC.Collect()
Console.WriteLi ne("Memory used after cleanup : " &
GC.GetTotalMemo ry(True))

Console.ReadLin e()
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*******@hotm ail.com> wrote in message news:e5******** ******@tk2msftn gp13.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.WriteLi ne("Memory used before object creation : " & GC.GetTotalMemo ry(True))

Dim obj1 As New Object
Console.WriteLi ne("Memory used after object 1 creation : " & GC.GetTotalMemo ry(True))

Dim obj2 As New Object
Console.WriteLi ne("Memory used after object 2 creation : " & GC.GetTotalMemo ry(True))

obj1 = Nothing
GC.Collect()
Console.WriteLi ne("Memory used after cleanup : " & GC.GetTotalMemo ry(True))

Console.ReadLin e()
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
3665
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 256 MB of Ram while the other has 2 GB of Ram. On the machine with less Ram, the process takes about 1 MB of Ram. On the machine with more Ram, it uses 9 MB of Ram. Is this normal and expected behavior?
5
6072
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 loss even though I seem to be allocaitng everything; help would be *vastly* appreciated. What am I doing wrong here? I thought I was doing everything correctly (setting things to null, for example) but none of the memory seems to get replaced. ...
2
460
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 the app I load some things into memory for global use of the app but I'll use only 2 starting forms to explain the situation) situation 1 start app with form 1 (72mb memory usage), show form 2 and hide form 1 (89 mb memory usage
6
3271
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, no memory is lying around. GC reports only 84k of allocations. Starting 5-10 of this apps is going to start taking a considerable amount of memory. Is there a way to reduce this? Tom
2
422
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 test the stuff, I keep to open that child data form for about 10 times. the memory usage shown in GC and task manager both increase. Then I close all those forms. and perform GC collect. The memory usage shown in GC falls, however, the memory...
3
4135
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 periodically. What happens, is that the app reads the contents of a text file line by line into an ArrayList. Each element of the ArrayList is a string representing a record from the file. The ArrayList is then processed, and the arraylist goes out of...
20
4215
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 memory usage drops to a couple hundred KB. Why? Is there anything I should to to prevent this? I have compiled in release and deactivated all forms of debugging, I think! Thanks, Philip
13
2847
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. It's most possibly caused by trac and it's dependencies, but several components of the OS where updated, too.
1
1721
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 = {} i = 1000*1000 while i 0:
1
2034
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 the objects for which that memory was allocated are no longer live in the process. This is only a leak if peak memory goes up again each time you create any new objects. Try repeated allocations of a large dictionary and observe how memory...
0
8421
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8844
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8742
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8518
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8621
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7354
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.