简体   繁体   English

单字符串列表的 memory 分配率高于多字符串

[英]Higher memory allocation rates for List of single-character String than multi-character String

Consider the following benchmark which allocates List of String of length 1 versus of length 8考虑以下基准,它分配长度为 1 与长度为 8 的String List

@State(Scope.Benchmark)
@BenchmarkMode(Array(Mode.Throughput))
class SoMemory {
  val size = 1_000_000
  @Benchmark def a: List[String] = List.fill[String](size)(Random.nextString(1))
  @Benchmark def b: List[String] = List.fill[String](size)(Random.nextString(8))
}

where sbt "jmh:run -i 10 -wi 10 -f 2 -t 1 -prof gc bench.SoMemory" gives其中sbt "jmh:run -i 10 -wi 10 -f 2 -t 1 -prof gc bench.SoMemory"给出

[info] Benchmark                                     Mode  Cnt           Score          Error   Units
[info] SoMemory.a                                   thrpt   20          16.650 ±        0.519   ops/s
[info] SoMemory.a:·gc.alloc.rate                    thrpt   20        3870.364 ±      120.687  MB/sec
[info] SoMemory.a:·gc.alloc.rate.norm               thrpt   20   255963282.822 ±       61.012    B/op
[info] SoMemory.a:·gc.churn.PS_Eden_Space           thrpt   20        3862.090 ±      161.598  MB/sec
[info] SoMemory.a:·gc.churn.PS_Eden_Space.norm      thrpt   20   255331784.446 ±  4839869.981    B/op
[info] SoMemory.a:·gc.churn.PS_Survivor_Space       thrpt   20          25.893 ±        1.433  MB/sec
[info] SoMemory.a:·gc.churn.PS_Survivor_Space.norm  thrpt   20     1711320.051 ±    64870.177    B/op
[info] SoMemory.a:·gc.count                         thrpt   20         318.000                 counts
[info] SoMemory.a:·gc.time                          thrpt   20       45183.000                     ms
[info] SoMemory.b                                   thrpt   20           2.859 ±        0.092   ops/s
[info] SoMemory.b:·gc.alloc.rate                    thrpt   20        2763.961 ±       89.654  MB/sec
[info] SoMemory.b:·gc.alloc.rate.norm               thrpt   20  1063705990.899 ±      503.169    B/op
[info] SoMemory.b:·gc.churn.PS_Eden_Space           thrpt   20        2768.433 ±      101.742  MB/sec
[info] SoMemory.b:·gc.churn.PS_Eden_Space.norm      thrpt   20  1065601049.380 ± 25878705.006    B/op
[info] SoMemory.b:·gc.churn.PS_Survivor_Space       thrpt   20          20.838 ±        1.063  MB/sec
[info] SoMemory.b:·gc.churn.PS_Survivor_Space.norm  thrpt   20     8015328.037 ±   236873.550    B/op
[info] SoMemory.b:·gc.count                         thrpt   20         234.000                 counts
[info] SoMemory.b:·gc.time                          thrpt   20       37696.000                     ms

Note how smaller string has significantly higher gc.alloc.rate请注意较小的字符串如何显着提高gc.alloc.rate

SoMemory.a:·gc.alloc.rate         thrpt   20        3870.364 ±      120.687  MB/sec
SoMemory.b:·gc.alloc.rate         thrpt   20        2763.961 ±       89.654  MB/sec

Why there seems to be higher memory consumption in the first case when smaller string should have smaller memory footprint, for example, JOL gives for为什么在第一种情况下,当较小的字符串应该具有较小的 memory 占用空间时,memory 的消耗似乎更高,例如, JOL给出了

class ZarA { val x = List.fill[String](1_000_000)(Random.nextString(1)) }
class ZarB { val x = List.fill[String](1_000_000)(Random.nextString(8)) }

as expected smaller footprint of approx 72MB for ZarA正如预期的那样,ZarA 的占用空间更小,约为ZarA

example.ZarA@15975490d footprint:
     COUNT       AVG       SUM   DESCRIPTION
   1000000        24  24000000   [C
         1        16        16   example.ZarA
   1000000        24  24000000   java.lang.String
   1000000        24  24000000   scala.collection.immutable.$colon$colon
         1        16        16   scala.collection.immutable.Nil$
   3000002            72000032   (total)

compared to larger footprint of approx 80MB for ZarBZarB大约 80MB 的更大占用空间相比

example.ZarB@15975490d footprint:
     COUNT       AVG       SUM   DESCRIPTION
   1000000        32  32000000   [C
         1        16        16   example.ZarB
   1000000        24  24000000   java.lang.String
   1000000        24  24000000   scala.collection.immutable.$colon$colon
         1        16        16   scala.collection.immutable.Nil$
   3000002            80000032   (total)

VisualVM memory behaviour VisualVM memory 行为

ZarA - used heap 129 MB ZarA - 使用的堆 129 MB

在此处输入图像描述

ZarB - used heap 91 MB ZarB - 已用堆 91 MB

在此处输入图像描述

Allocation rate is how fast you can allocate memory (amount of memory allocated per unit of time).分配率是指您可以多快分配 memory(每单位时间分配的 memory 的数量)。 It doesn't tell us anything about total memory allocated.它没有告诉我们任何有关分配的总 memory 的信息。

It is always easier to find smaller continuous memory region that larger contiguous memory region, so eg allocating eg 1000 Strings of length 1 should take impropotionaly less time than allocating eg 1000 String of length 8, resulting in higher allocation rate with less total memory consumption. It is always easier to find smaller continuous memory region that larger contiguous memory region, so eg allocating eg 1000 Strings of length 1 should take impropotionaly less time than allocating eg 1000 String of length 8, resulting in higher allocation rate with less total memory consumption.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将单字符字符串转换为 char - Convert single-character string to char 在StringBuffer追加中使用字符而不是String来表示单字符值 - Using character instead of String for single-character values in StringBuffer append 在Java中将int(字节)转换为单字符字符串的最简单方法 - Easiest way to convert an int (byte) to a single-character String in Java 使用char而不是字符串来表示单字符值有什么好处吗? - Is there any benefit to using char instead of string for single-character values? 使用 Swift 中的多字符字符串拆分 Swift 字符串 2 - Splitting a Swift String using a multi-Character String in Swift 2 JAVASCRIPT:是否有更快的方法来通过多字符定界符来分割此字符串? - JAVASCRIPT:Is there a faster way to get this string split by a multi-character delimiter? 在 C 中用多字符分隔符拆分 char 字符串 - split char string with multi-character delimiter in C 使用多字符分隔符将字符串拆分为数组 - Split string into array using multi-character delimiter 如何在Python中扫描多字符多字符串数组中的字符? - How do you scan characters in multi-character multi-string arrays in Python? C ++在编译时将字符串文字转换为多字符文字 - C++ convert string literal to multi-character literal at compile time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM