java-sizeof icon indicating copy to clipboard operation
java-sizeof copied to clipboard

over estimate size of array

Open oucyang opened this issue 4 years ago • 1 comments

next code will over estimate size of array, for example, object A contains array B, C is one element of B。 If C contains A, then, estimate C will get the over size.

private def visitArray(array: AnyRef, cls: Class[_], state: SearchState) {
...
        for (i <- 0 until ARRAY_SAMPLE_SIZE) {
          val elem = JArray.get(array, rand.nextInt(length))
size += SizeEstimator.estimate(elem, state.visited)
...

oucyang avatar May 13 '21 09:05 oucyang

code example is next

class Node(var id: Int, var tail: Node, var nexts: Array[Node])

  @org.junit.Test
  def TestSize(): Unit = {
    val buildCase = (total: Int) => {
      val num = total * 2
      var nodes = Array[Node]()
      for (i <- 0 until num) {
        nodes :+= new Node(id = i, tail = null, nexts = Array[Node]())
      }
      for (i <- 0 until num) {
        for (j <- 0 until total) {
          val id = Random.nextInt(num)
          nodes(i).nexts :+= nodes(id)
        }
      }
      nodes(0)
    }
    val testNum = 5
    for (i <- 1 to testNum) {
      val num = i * 100
      val n0 = buildCase(num)
      println(s"size $num ${SizeEstimator.estimate(n0)}")
    }
  }

oucyang avatar May 13 '21 09:05 oucyang