trimToSize

expect fun trimToSize()(source)

Attempts to reduce the storage used for this list.

If the backing storage of this list is larger than necessary to hold its current elements, then it may be resized to become more space efficient. This operation can help reduce memory consumption when the list is not expected to grow further.

On the JS target, this method has no effect due to the internal JS implementation.

Since Kotlin

1.0

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   val list = ArrayList<Int>(1000)
// Add only a few elements
list.addAll(listOf(1, 2, 3))
println(list) // [1, 2, 3]

// The list has capacity for 1000 elements, but only 3 are used.
// trimToSize() can help reduce memory usage by resizing the backing storage
// to fit the actual number of elements.
list.trimToSize()

// The list content remains the same
println(list) // [1, 2, 3]
assertEquals(3, list.size) 
   //sampleEnd
}
actual fun trimToSize()(source)

Does nothing in this ArrayList implementation.

Since Kotlin

1.1
actual fun trimToSize()(source)

Attempts to reduce the storage used for this list.

If the backing storage of this list is larger than necessary to hold its current elements, then it may be resized to become more space efficient. This operation can help reduce memory consumption when the list is not expected to grow further.

Since Kotlin

1.3

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   val list = ArrayList<Int>(1000)
// Add only a few elements
list.addAll(listOf(1, 2, 3))
println(list) // [1, 2, 3]

// The list has capacity for 1000 elements, but only 3 are used.
// trimToSize() can help reduce memory usage by resizing the backing storage
// to fit the actual number of elements.
list.trimToSize()

// The list content remains the same
println(list) // [1, 2, 3]
assertEquals(3, list.size) 
   //sampleEnd
}
actual fun trimToSize()(source)

Attempts to reduce the storage used for this list.

If the backing storage of this list is larger than necessary to hold its current elements, then it may be resized to become more space efficient. This operation can help reduce memory consumption when the list is not expected to grow further.

Since Kotlin

1.8

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   val list = ArrayList<Int>(1000)
// Add only a few elements
list.addAll(listOf(1, 2, 3))
println(list) // [1, 2, 3]

// The list has capacity for 1000 elements, but only 3 are used.
// trimToSize() can help reduce memory usage by resizing the backing storage
// to fit the actual number of elements.
list.trimToSize()

// The list content remains the same
println(list) // [1, 2, 3]
assertEquals(3, list.size) 
   //sampleEnd
}
actual fun trimToSize()(source)

Attempts to reduce the storage used for this list.

If the backing storage of this list is larger than necessary to hold its current elements, then it may be resized to become more space efficient. This operation can help reduce memory consumption when the list is not expected to grow further.

Since Kotlin

1.8

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   val list = ArrayList<Int>(1000)
// Add only a few elements
list.addAll(listOf(1, 2, 3))
println(list) // [1, 2, 3]

// The list has capacity for 1000 elements, but only 3 are used.
// trimToSize() can help reduce memory usage by resizing the backing storage
// to fit the actual number of elements.
list.trimToSize()

// The list content remains the same
println(list) // [1, 2, 3]
assertEquals(3, list.size) 
   //sampleEnd
}