小编C. *_*mar的帖子

C#,为什么GC每秒运行几次?

我正在开发一个创建交互式图表的程序.但是,即使禁用了程序的渲染层,也会出现以下问题.

在我的应用程序的某些屏幕上,根据Visual Studio 2015诊断工具,GC每秒大约运行4次,从而导致应用程序的性能下降(从120fps降至15fps).

我拿了一些期望看到意外分配的内存快照,但根据快照,每隔几秒就只有一两个System.Internal.HandleCollector + HandleType的分配和集合,这似乎是正常的,即使问题没有发生.

我注意到的其他一些事情:

  • 这发生在多台机器上.
  • 无论是否附加调试器,都会发生这种情况.
  • 应用程序的大部分CPU时间都在clr.dll中.
  • 每次GC运行的原因都列为"小对象堆分配",即使快照中没有可观察的分配.

在这一点上,我很难过.有没有人看到这种情况发生或知道我应该在哪里开始调试?

.net c# garbage-collection

9
推荐指数
2
解决办法
4868
查看次数

YouTube API:如何获取liveChatId?

文件说:

"liveChatId参数指定将返回其消息的聊天的ID.与广播相关联的实时聊天ID将在liveBroadcast资源的snippet.liveChatId属性中返回."

但是当使用API​​ Explorer和youtube.liveBroadcasts.list获取liveBroadcast的片段时,返回的结果上没有liveChatId属性.

我正在使用以下URI(键省略):

https://www.googleapis.com/youtube/v3/liveBroadcasts?part=snippet&mine=true

我究竟做错了什么?

这是响应(某些值替换为...):

{
 "kind": "youtube#liveBroadcastListResponse",
 "etag": "...",
 "pageInfo": {
  "totalResults": 1,
  "resultsPerPage": 5
 },
 "items": [
  {
   "kind": "youtube#liveBroadcast",
   "etag": "...",
   "id": "...",
   "snippet": {
    "publishedAt": "2016-04-18T17:04:24.000Z",
    "channelId": "...",
    "title": "...",
    "description": "...",
    "thumbnails": {
     "default": {
      "url": "...",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "...",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "...",
      "width": 480,
      "height": 360
     },
     "standard": {
      "url": "...",
      "width": 640,
      "height": 480
     },
     "maxres": {
      "url": …
Run Code Online (Sandbox Code Playgroud)

youtube youtube-api youtube-livestreaming-api

6
推荐指数
2
解决办法
3848
查看次数

Array.Sort() 分配大量内存

我正在开发一个以 60fps 运行的可视化。该可视化的一部分是根据项目的位置对屏幕上的项目进行排序。我在用着

Array.Sort<T>(T[] array, int index, int length, IComparer<T> comparer)
Run Code Online (Sandbox Code Playgroud)

每秒分配近 1MB 的内存Comparison<T>,这会导致 GC 频繁运行,从而导致帧速率出现问题。

我已经尝试了几种变体Array.Sort,它们都在分配,包括接受的变体Comparison<T>(这也是不够的,因为它缺少indexlength参数)。

有没有办法在 C# (.NET 5) 中对数组进行排序而不分配大量内存?

更新:这是一个重现,

using System;
using System.Collections.Generic;

namespace New_folder
{
    public class EmptyClass
    {
        // Empty
    }

    public class EmptyClassComparer : IComparer<EmptyClass>
    {
        public int Compare(EmptyClass x, EmptyClass y)
        {
            return 0;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            EmptyClass[] emptyClasses = new EmptyClass[100];
            for (int …
Run Code Online (Sandbox Code Playgroud)

c# performance memory-efficient .net-5

5
推荐指数
1
解决办法
1045
查看次数