java-sizeof
java-sizeof copied to clipboard
over estimate size of array
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)
...
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)}")
}
}