-1

In the unmanaged languages like C++ I can delete object using delete operator. But .NET platform is managed, so only the garbage collector can delete object from memory. We can only remove all references to the object and Garbage Collector will collect it sometime. But I want to perform instant delete of object.

Is there a way delete object using C++ library? The main idea:

  1. Create C++ library
  2. Define method DeleteUnsafe(void* pointer) that uses mscoree.dll to delete object
  3. Refer to the C++ library from .NET using DllImport attribute

Is it possible to perform second step? Or use some another way to achieve my goal? What the start point?

Is it possible to perform without corrupting CLR? For example, GC will try collect already deleted object. How we can remove the object from GC queue?

Viktor Lova
  • 4,776
  • 2
  • 19
  • 25
  • 2
    `GC.Collect()` is the closest you will get, but you would need a very good reason molest the built-in garbage collection mechanic. What's your reason for wanting to do so? – Alex K. Sep 12 '14 at 11:42
  • "I want to perform instant delete of object". Are you sure you need to? Why? – spender Sep 12 '14 at 11:43
  • @AlexK. suppose that I want to write alternative garbage collector – Viktor Lova Sep 12 '14 at 11:44
  • Your alternative garbage collector would need to rely on `GC.Collect` which has no "delete a single object" functionality. It just means "collect *everything* that is eligible" which carries significant overhead. – spender Sep 12 '14 at 11:47
  • @spender no, you don't get idea. I want to find hack on CLR platform to achieve my goal. I understand that I can't achieve this using only C# or another language over CLR – Viktor Lova Sep 12 '14 at 11:48
  • 2
    Ah. Hacking the CLR. I'll stand back. Seems far-fetched though. – spender Sep 12 '14 at 11:50
  • @spender maybe while hacking the clr, he can patch some other known issues, and maybe give the rest the ability to delete specific objects ? :) – Noctis Sep 12 '14 at 11:53
  • 1
    Well, did you download the .NET source code to study how objects are actually managed? That's what I would suggest. When you actually find a possible hacking entry point, come back here with code. – Patrice Gahide Sep 12 '14 at 11:59
  • 2
    Anything is possible. Nothing is practical. It has been done before by very smart guys and [it sucked rocks](http://www.sellsbrothers.com/Posts/Details/1141). The .NET GC got a lot more respect after that. – Hans Passant Sep 12 '14 at 12:12
  • @PatriceGahide .NET source code? Do you mean native source code of .NET framework? Is it available? How you think, why I ask the question? Because I can't google information about it, I just don't know where to start. Stackoverflow is the site to ask questions. Is my question is bad? Does it look like I want you to solve my hometask? Why guys minused it? – Viktor Lova Sep 12 '14 at 12:19
  • @nsinreal I didn't mean to be sarcastic. If that's what you thought, sorry about that. I was not talking about the official .NET implementation (for which you can find the _library_ source code, e.g. GC [here](http://referencesource.microsoft.com/#mscorlib/system/gc.cs)) but more of something like [Mono](http://www.mono-project.com/download/), about which you'll find every detail you may need. – Patrice Gahide Sep 12 '14 at 12:32

1 Answers1

0

Not too sure about the why you want to do this, but I'll assume you have a reason.

What comes to mind would be forcing garbage collection, but here's a quote from MS:

"It is possible to force garbage collection by calling Collect, but most of the time, this should be avoided because it may create performance issues. "

(you can have a look here for more: https://stackoverflow.com/a/233647/1698987.

Other than that, if you really need that fine control, maybe c# ain't the answer?

hmm ... have a look at this msdn page. Seems that calling GC.Collect method assuming that you know what you're doing isn't that bad after all... :)

Community
  • 1
  • 1
Noctis
  • 11,507
  • 3
  • 43
  • 82