473,769 Members | 1,980 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

About Best Coding style of c#

List<string> effectList = new List<string>();
effectList.Clea r();
effectList = null;

using (List<string> effectList = new List<string>())
{
}

If there are so many calls, I should save as much memory as I can.

If I want to allocate & release some NOT "disposable " elements, is it
more preferable to do clear and null allocation after use?

or use it with using phrase?

or just leave it alone?

Of course I don't want to coding "disposable " library by my self.

Jun 17 '06 #1
1 1462
"kiplring" <ki********@hot mail.com> wrote:
List<string> effectList = new List<string>();
effectList.Clea r();
effectList = null;
You don't need to call Clear() if you're going to null it out on the
next line.
using (List<string> effectList = new List<string>())
{
}
Note that this won't work. The nearest equivalent is:

---8<---
{
List<string> effectList = new List<string>()
}
--->8---

.... since List<T> doesn't implement IDisposable.
If there are so many calls, I should save as much memory as I can.

If I want to allocate & release some NOT "disposable " elements, is it
more preferable to do clear and null allocation after use?
If you have a long-lived frame on the stack, and it keeps a large chunk
of memory alive, then it makes sense to null it out. For example:

A() calls
B(), which loops and calls
C(), which loops and calls
D() which loops

where A() looks like:

A():
Allocate memory for a, which is big
Do stuff with a
B()
if (x)
Do stuff with a

Here, the stack frame for A() is going to last for a long time, and the
JIT compiler might not free up 'a' automatically if it can't figure out
'x'. If:

1) The object graph being pointed to by 'a' is big, as in a significant
fraction of your total memory usage, and
2) you know before calling B() that x will be false,
3) it may be useful then to set 'a' to null.

The graph pointed to by 'a' won't be collected until the next GC. If the
only thread running is your thread, and the functions B, C and D don't
allocate memory or only allocate a little, chances are a GC won't even
happen.
or use it with using phrase?

or just leave it alone?


With respect to disposal, it's much more important to focus on actual OS
resources, such as FileStreams and the like. For most little bits of
memory, it isn't worth the trouble to worry about.

If you're concerned about these things, it's best to profile your
application. Write it in the most natural way, and profile it later.
That way, you build up experience that can help you tune things. You
don't need extra tools to profile memory usage. Performance Monitor can
tell you how much time is being spent in GC, to find out if memory
allocation is a speed hog. SOS (Google ".load sos") along with
"!dumpheap -stat", "!dumpheap -type <type>" and "!gcroot" can help track
down what kinds of objects are alive, and where they're being kept alive
from, if you find out that too much memory seems to be allocated.

In general, it isn't worth the effort to micromanage individual memory
allocations below the 80 KB limit. Above 80 KB, (i.e. the total size of
an individual allocation exceeds 80 KB, such as "new byte[100000]") the
chunks of memory are allocated from the large object heap and don't get
collected until gen2 collections. For this reason, it can make sense to
pool such large allocations.

-- Barry

--
http://barrkel.blogspot.com/
Jun 17 '06 #2

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

Similar topics

125
14841
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
144
6944
by: Natt Serrasalmus | last post by:
After years of operating without any coding standards whatsoever, the company that I recently started working for has decided that it might be a good idea to have some. I'm involved in this initiative. Typically I find that coding standards are written by some guy in the company who has a way of coding that he likes and then tries to force everybody else to write code the way he likes it, not for any rational reason, but simply for the...
42
1958
by: kiplring | last post by:
1. int intArr = new int; 2. int intArr = new int; rgnNumberArr.Initialize(); 3. int intArr = new int; for( int i=0; i<intArr .lenght; i++)
10
1430
by: kiplring | last post by:
I made a input window derived from System.Windows.Forms.Form. What I use is just one property "inputWindow.Message". But System.Windows.Forms.Form has so many properties, functions, evnets. So I want to hide all things except the one property "inputWindow.Message".
13
1593
by: charpour | last post by:
Hello, in the program below: #include <stdio.h> int main(void) { int x = 1, y = 2, z; z = x+++y; return 0; }
0
9579
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
9416
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10032
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
9979
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
9849
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
8861
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...
1
7393
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5293
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...
0
5433
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.