Skip to content

There's no way to get the managed object size in memory #24200

Open
@danmoseley

Description

@danmoseley

Public API to detect the size of the managed object.

As mentioned in this article, we don't have such API to get the size in the runtime.

So far, if we want to do so, we have a choice to use some tools or techniques (not at the runtime), such as:

  • using dotMemory.
  • using WinDBG and calculate the object fields one by one.
  • measuring the GC before and after ccreating the objects.

On of the very good example of that need, in the case we build customized cache and we want to keep the maximum size limit under control.

The developers around the world still asking and requesting such API.

How we can implement such thing

The good news that we already have this mechanism already implemented internally by SizedReference Type.

internal class SizedReference : IDisposable
{	
	//some code here

	public SizedReference(Object target) { ... }
	
	public Object Target { get {...} }

	public Int64 ApproximateSize { get { ... } }
	
	public void Dispose() {}
}

What we gonna do is just to expose it as Public API

Proposed API

I suggest to add some class under System.Runtime to as a wrapper to SizedReference

or just as extension to the object.

/// <summary>
/// Get the approximate size allocated for this object in the memory in bytes
/// </summary>
/// <param name="obj">the object that you want to know its size</param>
/// <returns>size in bytes</returns>
public static long GetSize(this object obj)
{
	//using SizedReference Type to get the approximate size
}


@alden-menzalgy commented on Sun Nov 05 2017

Hi,

There is no way to get the actual (or even an approximate) size of the cached object in the memory.

The case is, we have +100 customized cache for different purposes to accelerate our platform, for each cache we have

  • Cache MaxSize
  • Cache ConsumedSize
  • Cache RemainingSize

Whenever we insert some object to be cached, we should detect its size to re-calculate aforementioned cache properties.

I know that's a complex issue and depends on many factors, so far we have some workarounds but non of them is official and we afraid to be changed in any minor or major release.

Should you add any API to get the object size in the memory ? or at least Type size and then we can add the object-specific data length.

Related Topics

Workaround 1
What Microsoft says about this issue


@JanEggers commented on Sat Nov 11 2017

you can use structs for your cache items and Marshal.Sizeof


@alden-menzalgy commented on Mon Nov 13 2017

It requires changing the structure that we've built the application with.
It may consider a workaround but not permanent solution

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-needs-workAPI needs work before it is approved, it is NOT ready for implementationarea-System.Runtimehelp wanted[up-for-grabs] Good issue for external contributors

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions