|
correct.
Setting a reference to null is useful in two similar situations:
1) as a class member, when the member is no longer needed whereas the object itself is still alive;
example: a class representing a compiler, it first parses the input and needs a keyword list for that, then it generates output code, for which the keywords are no longer useful, so the list could be nulled halfway.
2) as a local variable in a method, when the method basically has two parts, and the second half doesn't need all the data that existed in the first half.
Otherwise setting a reference to null doesn't make much sense (i.e. a class member at the end-of-life of the object, or a local variable near the end of the method).
BTW: the same applies to files and streams; it does not make sense to flush them when the next thing you do is close them.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
Trollslayer wrote: list1.RemoveAt(0); // This removes the reference but does it remove the object?
of course it does NOT delete the object, as:
1) RemoveAt() operates on the list, not on the object; how could it, it has no way of knowing whether the object is still required somewhere (e.g. when another reference exists somewhere, maybe in the same or in another list);
2) the GC is typically not invoked by calling a method that removes, deletes, closes, or shrinks something; it may be called by an action that creates a need for memory, most notably the new keyword.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
Trollslayer wrote: Any suggestions as to how I can force removal of the object or prove that it has been removed?
Maybe it's possible to (ab)use a WeakReference to prove that the object has not died yet, but you couldn't accurately use that trick to monitor the lifetime of the object (you could at best poll the IsAlive property in a tight loop, but the time measurement will be inaccurate)
Calling GC.Collect may help to kill the object ealier, but this function only gives a "best effort" guarantee: "Use this method to force the system to try to reclaim the maximum amount of available memory"
|
|
|
|
|
Here's myOpinion:
The main thing to consider is avoiding very large objects. Having large object *graphs* is not a problem, but if a single object is huge the GC will pin it to a memory location and not move it around.
The typical culprit is a huge string. So if for example you have a lot of xml data you want to keep in memory, it's probably better to load a document and keep that in memory than just the string. The document obviously requires much more space, but it is a graph with lots of objects and the memory manager can easily move them around as needed to defragment memory.
The second thing to consider is to avoid unnecessary allocation and deallocation. It looks to me like your code is replacing one object with another object. If the two are of the same type, why not simply change the state of the original object instead of allocating a new one?
Of course, the total amount of memory needed to run your application is only dependant on live instances, but if you put massive pressure on the memory manager everything will slow down because the GC will have to collect garbage very often and perhaps defrag often as well. So if you're getting out of memory exceptions you either simply use too much memory or you have some pinned objects. Pinned objects can cause out of memory exceptions to occur when superficially it seems you have enough memory available. For example, trying to allocate a 1MB string could fail even if there's 100MB available memory, if no continous block of 1MB is available (or can be made available by defragmenting the memory - hence this can only happen if there are pinned objects).
In short: Don't try to "manage" the memory. The GC will do a far better job with that than you can, because the GC has all the information necessary available to it, such as installed system memory, memory used by other appdomains, allocs per second, live instances, how fragmented the memory is, and so on.
|
|
|
|
|
OK, you've all been a great help and this is what I've learned/decided:
ArrayList is not as efficient as List but I will use it for the output table list since it there will be a number of different table types and as long as they have a large number of common methods it is an easy method of keeping the tables in sequence (this is very important for my application).
You have to treat the LOH with respect - I'm using VS2005 for competibility reasons so I don't know if the fragmentation issue is present.
Although you can try to force garbage collection it looks like this is likely to cause more problems than it solves in my case.
Collections use arrays which double to cope with increases in size can cause problems but since I am using collections of objects then it is the references that double so the object overhead probably isn't too bad.
The using statement looks interesting, I'll consider it for the second version of my stream analysis engine.
Reuse of objects - yes, in an application such as the one I am working on processing 4GB video (DBV actually) files the singleton principle of creating an object only if required and then reusing it is very important since I will have arrays of arrays of table - not a two dimensional array of tables.
Oh, there will definitely be a second version of the analysis engine but as the application is commpany confidential I'm not sure what will go into my first article, probably a beginners guide to multi threading user interfaces which as it's own oddities for a beginner to muddle though.
Thanks everyone.
|
|
|
|
|
Trollslayer wrote: ArrayList is not as efficient as List but I will use it for the output table list since it there will be a number of different table types and as long as they have a large number of common methods it is an easy method of keeping the tables in sequence
If it's just the common methods that you need to access via the array list then you can improve matters by using an interface and having a list of that.
public interface ITable
{
int IntProperty { get; set; }
void RandomMethod();
}
public class TableA : ITable
{
public int IntProperty
{
get;
set;
}
public void RandomMethod()
{
}
}
public class TableB : ITable
{
public int IntProperty
{
get;
set;
}
public string StringProperty
{
get;
set;
}
public void RandomMethod()
{
}
}
List<ITable> tableList = new List<ITable>(100);
tableList.AddRange(new ITable[]{
new TableA(),
new TableB()
});
|
|
|
|
|
Thanks Dave, I will be using common methods a lot of the time but have to allow for unique methods as well - DVB is fine until broadcasters get their hands on it - oh, until various small countries have their own needs and get their hands on it etc.
|
|
|
|
|
The problem with standards is there's so many of them
|
|
|
|
|
hi
i need to to get Bios date from c# in win64 environments.
any explicit idea? thanks
|
|
|
|
|
Why are asking the same question again ?
Abhijit Jana | Codeproject MVP
Web Site : abhijitjana.net
Don't forget to click "Good Answer" on the post(s) that helped you.
|
|
|
|
|
its the first time that i am asking about Biosd Date in Win64 environmet,
anyhow, please answer if possible. thanks.
|
|
|
|
|
faraz34 wrote: its the first time that i am asking about Biosd Date in Win64 environmet,
Not true, you already asked it once below here!
|
|
|
|
|
|
DateTime.Now should do it.
Vuyiswa Maseko,
Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code.
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vuyiswamaseko.com
vuyiswa@its.co.za
http://www.itsabacus.co.za/itsabacus/
|
|
|
|
|
DateTime.Now should do. There is not difference between BIOS and your system's date time. Any time you make changes in your system clock, it updates the BIOS date time as well.
It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD
|
|
|
|
|
Are you trying to get the system date/time clock or are you trying to get the Timestamp or Verison of the BIOS itself?
|
|
|
|
|
Hello dears
when I am making a crystal report After I add a text object from toolbox to report headerI want to write a code that can put a value of a field of a table to text object. Does anybody know how can I do it?
(I do apologize if I have a grammatically error in English language, I don’t know English well)
|
|
|
|
|
this operate excellent for win32 base programms,
but for win64 cant return biosdate. any idea? thanks
using System.Security.Permissions;
using Microsoft.Win32;
private string biosDate()
{
RegistryKey RK = Registry.LocalMachine;
RK = RK.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\");
Object Date = RK.GetValue("SystemBiosDate");
return Date.ToString();
}
|
|
|
|
|
SystemBiosDate key doesn't exist
only two letters away from being an asset
|
|
|
|
|
My Vista system's registry has such a SystemBiosDate entry, type REG_SZ, value 04/21/08, which for sure isn't the current date, however it could be the date the manufacturer (Dell) assembled and loaded my machine.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
Probably manufacturer specific. My Windows 7, nor Server 208 boxes doesn't have it.
only two letters away from being an asset
|
|
|
|
|
I am trying to do some events while the mouse button is down.
Here is what I have
private static bool IsKeyDown = false;
private void button1_MouseDown(object sender, MouseEventArgs e) {
IsKeyDown = true;
while (IsKeyDown) {
Winamp.Winamp.VolumeDown();
}
}
private void button1_MouseUp(object sender, MouseEventArgs e) {
IsKeyDown = false;
}
The problem is when it hits the mouse down event It continues to run the while loop and does not hit the mouse up event.
I don't want to use a timer to do this.
I did see some code that I think will work better in my app but I cant seem to get it to work can some one please help.
http://stackoverflow.com/questions/1152525/c-winform-looping-event-on-press-button[^]
private bool mouseDown = false;
private void buttonScrollUp_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
new Thread(() => {
while (mouseDown)
{
Invoke(new MethodInvoker(() => [DO_THE_SCROLLING_HERE));
Thread.Sleep([SET_AUTOREPEAT_TIMEOUT_HERE);
}
})
.Start();
}
private void buttonScrollUp_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
|
|
|
|
|
Zap-Man wrote: private static bool IsKeyDown = false;
Confused. Can this variable be changed ever? Correct me if I am wrong...
|
|
|
|
|
yes it changes on the mouse down to true. but the mouse up event never fires because the loop has not finished. but it will change in the mouse up event. if the value never changed then I would have to set it to const or read only. A static variable will change. private means this variable is not accessible from another class. when the form is initialized the variable is set to false. When the mouseDown event is fired then the variable is set to true. This is working. the problem lies in the loop. Because it is in the loop the MouseUp event never executes.
|
|
|
|
|
Zap-Man wrote: IsKeyDown = true;
while (IsKeyDown) {
Winamp.Winamp.VolumeDown();
}
This loop will never terminate so your event handler button1_MouseDown will never notify the framework that it is complete. This blocks your application from doing anything else, so you need to separate the functions somehow.
BTW it helps if you put <pre></pre> tags around your code snippet so it is easier to read.
|
|
|
|