一个方法的返回值如果不赋值给变量,是否会占用内存(在.NET 6中)?

huangapple go评论64阅读模式
英文:

Does a method's return value occupy memory if not put into a variable (in .NET 6)?

问题

I have these two C# 10 (.NET 6) methods:

public List<User> ReadAllFromCacheOrDb()
{
    // the logic inside this repository will implement the cache
    return _userRepository.ReadAllFromCacheOrDb();
}

private void SetCache()
{
    ReadAllFromCacheOrDb();
}

I was going to use SetCache() method whenever I wanted to update the cache like this:

public Task<User> Create(IdentityUser<long> entity)
{
    var result = _userRepository.Create(entity);
    SetCache();
    return result;
}

public User Update(IdentityUser<long> entity)
{
    var result = _userRepository.Update(entity);
    SetCache();
    return result;
}

Now I was wondering if the SetCache() method will occupy more memory than usual because of not putting the return value of ReadAllFromCacheOrDb() into any variable.

Will the garbage collector quickly dispose of things like this?

英文:

I have these two C# 10 (.NET 6) methods:

public List&lt;User&gt; ReadAllFromCacheOrDb()
{
    // the logic inside this repository will implement the cache
    return _userRepository.ReadAllFromCacheOrDb();
}

private void SetCache()
{
    ReadAllFromCacheOrDb();
}

I was going to use SetCache() method whenever I wanted to update the cache like this:

public Task&lt;User&gt; Create(IdentityUser&lt;long&gt; entity)
{
    var result = _userRepository.Create(entity);
    SetCache();
    return result;
}

public User Update(IdentityUser&lt;long&gt; entity)
{
    var result = _userRepository.Update(entity);
    SetCache();
    return result;
}

Now I was wondering if the SetCache() method will occupy more memory than usual because of not putting the return value of ReadAllFromCacheOrDb() into any variable.

Will the garbage collector quickly dispose of things like this?

答案1

得分: 0

现在我在想,如果不将ReadAllFromCacheOrDb()的返回值放入任何变量中,SetCache()方法会占用更多内存吗?

不管是否有变量,都不会有太大影响。编译器可能会将方法重写为静态单赋值形式,优化寄存器和堆栈空间的使用。

然而,返回的列表本身仍将分配在堆上,并且会一直保留在那里,直到被垃圾回收器释放。除非列表非常庞大,否则这通常不会成为问题,但如果您能避免在第一次创建列表,可能会有所帮助。

一般来说,如果您关心这些问题,可以使用内存和/或性能分析器。它们会告诉您是否存在问题以及问题出在哪里。开发人员往往会担心无关紧要的事情,并花费大量时间优化对性能影响较小的事情,而忽视了对性能有重大影响的问题。

英文:

> now I was wondering if the SetCache() method will occupy more memory than usual because of not putting the return value of ReadAllFromCacheOrDb() into any variable.

It does not really matter if you have a variable or not. The compiler will likely rewrite the method into static single assignment form, will optimize register and stack space usage.

However, the returned list itself will still be allocated on the heap, and will remain there until it is freed by the GC. This will likely not be a problem unless your list is huge, but it might help a little if you could avoid creating the list in the first place.

In general, use a memory and/or performance profiler if you are concerned about things like this. These will tell you if there is a problem, and where it is. It is very common for developers to worry about the wrong things, and spend a bunch of time optimizing things that have a minimal performance impact, while missing problems that do have a large impact.

答案2

得分: 0

垃圾收集器会迅速处理仅短暂使用的对象。它针对这种情况进行了优化。此外,对于这些对象,它只有最少的簿记,因此分配和回收的成本非常低。

如果由 ReadAllFromCacheOrDb() 创建的列表属于这种情况,那就不同了。
如果它很小,是的。创建它可能需要很少的时间,并且它将适应垃圾收集器的快速回收内存。

如果列表较长,它可能无法适应,或者在 ReadAllFromCacheOrDb() 内部花费了太多时间和内存,以至于触发垃圾收集器查看情况。它会进行一些清理,但会保留列表及其关联的对象。它们将不再存在于快速回收内存中,因此会变得昂贵。

昂贵到什么程度?只有通过测量才能告诉。如果这涉及的只是几千个对象,而且更新不频繁,我不会担心它。

英文:

The garbage collector quickly disposes of object's that are only used very briefly. It is optimized for that scenario. Also it has only minimal bookkeeping for those objects, so the cost of allocating and collecting is very low.

If the List that is created by ReadAllFromCacheOrDb() falls in that category is a different matter.
If it is small, yes. It probably took little time to create it and it will fit in the quick-to-collect memory of the garbage collector.

If the list is longer it might not fit, or to much time is spend and memory consumed inside ReadAllFromCacheOrDb() to trigger the garbage collector to look at the situation. It will do some cleanup, but keep the list and it associated objects. They will no longer live in the quick-to-collect memory and become expensive.

How expensive? Only measuring can tell. If this concerns only several thousands of objects and updates are not frequent I would not worry about it.

huangapple
  • 本文由 发表于 2023年5月15日 15:37:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76251830.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定