garbage-collection

Practical use of System.WeakReference

I understand what System.WeakReference does, but what I can't seem to grasp is a practical example of what it might be useful for. The class itself seems to me to be, well, a hack. It seems to me that there are other, better means of solving a problem where a WeakReference is used in examples I've seen. What's the canonical example of ...

Enforcing required function call

Hi, I have a "Status" class in C#, used like this: Status MyFunction() { if(...) // something bad return new Status(false, "Something went wrong") else return new Status(true, "OK"); } You get the idea. All callers of MyFunction should check the returned Status: Status myStatus = MyFunction(); if ( ! myStatus.IsOK() ...

Why Java and Python garbage collection methods are different?

Python uses reference count method to handle object life time. So an object has no more used and it will be immediately destroyed. But, in Java, GC(garbage collector) destroys objects which are no more used at specific time. Why Java choose this strategy and what is benefit from this? Is this better than Python approach? ...

Caching Schemes for Managed Languages

This is mostly geared toward desktop application developers. How do I design a caching block which plays nicely with the GC? How do I tell the GC that I have just done a cache sweep and it is time to do a GC? How do I get an accurate measure of when it is time to do a cache sweep? Are there any prebuilt caching schemes which I could ...

Why do you not explicitly call finalize() or start the garbage collector?

After reading this question, I was reminded of when I was taught Java and told never to call finalize() or run the garbage collector because "it's a big black box that you never need to worry about". Can someone boil the reasoning for this down to a few sentences? I'm sure I could read a technical report from Sun on this matter, but I th...

What's wrong with this code? If anything.

Essentially I want to know if in VB.NET 2005 if using a sqlcommand and then reusing it by using the NEW is wrong. Will it cause a memory leak. EG: try dim mySQL as new sqlcommand(sSQL, cnInput) // do a sql execute and read the data mySQL = new sqlcommand(sSQLdifferent, cnInput) // do sql execute and read the data catch ... final...

How do you know if you can use a patented algorithm?

I've been reading up on Sun's new Garbage First garbage collection algorithm, and was considering using it on a personal project. When googling for more information I found that it is patented, which raises the question if I could ever release my code. How can I tell whether this specific research is safe to learn from and use? Did Su...

Garbage Collection: Is it necessary to set large objects to null in a Dispose method?

Is it necessary to set large objects to null when implementing a Dispose method? ...

Handles vs. AddHandler

Is there an advantage to dynamically attaching/detaching event handlers? Would manually detaching handlers help ensure that there isn't a reference remaining to a disposed object? ...

Is SqlCommand.Dispose enough?

Can I use this approach efficiently? using(SqlCommand cmd = new SqlCommand("GetSomething", new SqlConnection(Config.ConnectionString)) { cmd.Connection.Open(); // set up parameters and CommandType to StoredProcedure etc. etc. cmd.ExecuteNonQuery(); } My concern is : Will the Dispose method of the SqlCommand (which is calle...

Garbage collectors for C++

What free and commercial garbage collection libraries are available for C++, and what are the pros and cons of each? I am interested in hard-won lessons from actual use in the field, not marketing or promotional blurb. There is no need to elaborate on the usual trade offs associated with automatic garbage collection, but please do ment...

What Prevents a Thread in C# from being Collected?

In .NET, after this code, what mechanism stops the Thread object from being garbage collected? new Thread(Foo).Start(); GC.Collect(); Yes, it's safe to assume something has a reference to the thread, I was just wandering what exactly. For some reason Reflector doesn't show me System.Threading, so I can't dig it myself (I know MS relea...

How to avoid garbage collection in real time C# application ?

I'm writting a financial C# application which receive messages from the network, translate them into different object according to the message type and finaly apply the application business logic on them. The point is that after the business logic is applied, I'm very sure I will never need this instance again. Rather than to wait for t...

Does the Java VM move objects in memory, and if so - how?

Does the Java virtual machine ever move objects in memory, and if so, how does it handle updating references to the moved object? I ask because I'm exploring an idea of storing objects in a distributed fashion (ie. across multiple servers), but I need the ability to move objects between servers for efficiency reasons. Objects need to b...

How to implement closures without gc?

I'm designing a language. First, I want to decide what code to generate. The language will have lexical closures and prototype based inheritance similar to javascript. But I'm not a fan of gc and try to avoid as much as possible. So the question: Is there an elegant way to implement closures without resorting to allocate the stack frame ...

Triggering a .NET garbage collection externally

Is there a way to trigger a garbage collection in a .NET process from another process or from inside WinDBG? There are the Managed Debugging Assistants that force a collection as you move across a native/managed boundary, and AQTime seems to have button that suggests it does this, but I can't find any documentation on how to do it. ...

Which loop has better performance? Why?

String s = ""; for(i=0;i<....){ s = some Assignment; } or for(i=0;i<..){ String s = some Assignment; } I don't need to use 's' outside the loop ever again. The first option is perhaps better since a new String is not initialized each time. The second however would result in the scope of the variable being limited to the loo...

Java garbage collection on VMWare servers

Has anyone experienced issues with Java's default garbage collector while running apps on VmWare instances? I am experiencing issues where full garbage collections are not running as often as I would expect and am curious if the VmWare variable has anything to do with it. ...

Resources that have to be manually cleaned up in C#?

What resources have to be manually cleaned up in C#? ...and what are the consquences of not doing so? For example, say i have the following code: myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black); //Use Brush If i don't clean up the brush using the dispose method, i'm assuming the garbage collector frees the memory ...

What's so wrong about using GC.Collect()?

Although I do understand the serious implications of playing with this function (or at least that's what I think), I fail to see why it's becoming one of these things that respectable programmers wouldn't ever use, even those who don't even know what it is for. Let's say I'm developing an application where memory usage varies extremely ...