custom memory management

  • Thread starter Thread starter Wiktor Zychla [C# MVP]
  • Start date Start date
W

Wiktor Zychla [C# MVP]

suppose I implement a custom caching mechanism for my custom ORM
implementation. suppose I'd like the client code to be able to define the
maximum amount of memory the cache is allowed to occupy.

however, I have no idea how from within the code I can even retrieve the
amount of memory an object takes. the System.GC.GetTotalMemory method seem
to retrieve the total amount of allocated memory while I would be interested
in monitoring the memory usage for a selected object.

what I need is just a right direction of research.

Thanks in advance
Wiktor Zychla
 
Wiktor,

The two ways I could think of doing this would be to hook into the
debugging interface in the CLR, or to provide a custom host which would be
able to report the memory consumed by your particular object(s).

The thing is though, why the need for this kind of mechanism? It's
surely more trouble than it's worth, and there is usually not a justifiable
need (not saying yours isn't, but it would help to know why, given that you
have GC to take care of memory management issues for you).
 
Wiktor Zychla said:
suppose I implement a custom caching mechanism for my custom ORM
implementation. suppose I'd like the client code to be able to define the
maximum amount of memory the cache is allowed to occupy.

however, I have no idea how from within the code I can even retrieve the
amount of memory an object takes. the System.GC.GetTotalMemory method seem
to retrieve the total amount of allocated memory while I would be
interested in monitoring the memory usage for a selected object.

First you must define the boundary of your object. Is it:

(A) the memory block on the heap which the reference points to
(B) the set of all blocks reachable from that object (i.e. consider the
object as a GC root)
(C) the set of all blocks which could be freed if the object was eliminated
from the cache
(D) something else
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top