为什么GC的minor_gc_count会减少?

6
我们有一个heroku应用程序。当我早上检查GC.stat时,GC.stat[:minor_gc_count]为51。当天晚些时候它变成了50。
据我理解,这应该是垃圾收集器完成小扫描的次数,所以第二天上升是有道理的,但为什么会下降呢?
>heroku run rails c --remote production
Running rails c on ⬢ ... up, run.2287 (Standard-1X)
Loading production environment (Rails 5.2.2.1)
irb(main):001:0> GC.stat
=> {:count=>63, :heap_allocated_pages=>1753, :heap_sorted_length=>1753, :heap_allocatable_pages=>0, :heap_available_slots=>714528, :heap_live_slots=>713742, :heap_free_slots=>786, :heap_final_slots=>0, :heap_marked_slots=>471239, :heap_eden_pages=>1753, :heap_tomb_pages=>0, :total_allocated_pages=>1753, :total_freed_pages=>0, :total_allocated_objects=>2802530, :total_freed_objects=>2088788, :malloc_increase_bytes=>65256, :malloc_increase_bytes_limit=>32225676, :minor_gc_count=>51, :major_gc_count=>12, :remembered_wb_unprotected_objects=>4626, :remembered_wb_unprotected_objects_limit=>8538, :old_objects=>458044, :old_objects_limit=>838856, :oldmalloc_increase_bytes=>65712, :oldmalloc_increase_bytes_limit=>19737900}
irb(main):002:0> exit
**Airbrake: closed
>heroku run rails c --remote production
Running rails c on ⬢... up, run.7226 (Standard-1X)
Loading production environment (Rails 5.2.2.1)
irb(main):001:0> GC.stat
=> {:count=>62, :heap_allocated_pages=>1618, :heap_sorted_length=>1913, :heap_allocatable_pages=>295, :heap_available_slots=>659511, :heap_live_slots=>659395, :heap_free_slots=>116, :heap_final_slots=>0, :heap_marked_slots=>467961, :heap_eden_pages=>1618, :heap_tomb_pages=>0, :total_allocated_pages=>1618, :total_freed_pages=>0, :total_allocated_objects=>2726093, :total_freed_objects=>2066698, :malloc_increase_bytes=>5662240, :malloc_increase_bytes_limit=>24780563, :minor_gc_count=>50, :major_gc_count=>12, :remembered_wb_unprotected_objects=>4632, :remembered_wb_unprotected_objects_limit=>9262, :old_objects=>456572, :old_objects_limit=>913146, :oldmalloc_increase_bytes=>7549584, :oldmalloc_increase_bytes_limit=>19737900}

major_gc_count 也开始出现这种情况。通常是12,但有时会变成13,然后又回到12。今天 minor_gc_count 也达到了49。 - Marlin Pierce
1个回答

1
问题可能出在测试本身。当你运行GC.stat时,它会返回关于当前正在运行的进程的信息。这很好。问题在于每次运行时。
heroku run rails c --remote production

在控制台中,它不会连接到您当前在Heroku上运行的应用程序的进程。它将为控制台 启动一个新进程,并为此新创建的进程返回GC.stat,而不是处理Web请求的进程。这就是它如此低甚至可能下降的原因。

您实际上可以自行测试。当您在Heroku上连接到rails控制台时,请运行以下Ruby代码:

Process.pid

它将返回当前进程的ID。然后断开与Heroku的连接,重新连接并再次运行Process.pid。您会发现,它将返回不同的进程ID,因为在您从控制台断开并创建新连接时,它停止了先前的进程,并为新连接创建了新的进程。
您还可以尝试在这些连接中运行GC.stat,您会发现它们可能不同,并且计数可以在这些连接之间上下波动,这是因为这些进程不相互依赖。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接