Skip to content

Instantly share code, notes, and snippets.

@amaembo
Created October 25, 2021 10:04
Show Gist options
  • Save amaembo/371aa7fb7ae472bb5a671ecf6dfa46f0 to your computer and use it in GitHub Desktop.
Save amaembo/371aa7fb7ae472bb5a671ecf6dfa46f0 to your computer and use it in GitHub Desktop.
MemoryTest sample
import java.util.ArrayList;
public class MemoryTest {
public static void main(String[] args) {
var processors = new ArrayList<Processor>();
for (int i = 0; i < 1000; i++) {
int[] data = new int[10_000_000];
processors.add(new Processor(data) {
@Override
int calculate() {
return length;
}
});
}
System.out.println(processors.stream().mapToLong(Processor::calculate).sum());
}
}
abstract class Processor {
int length;
Processor(int[] data) {
length = data.length;
}
abstract int calculate();
}
import java.util.ArrayList
import kotlin.jvm.JvmStatic
import java.util.function.ToLongFunction
object MemoryTest {
@JvmStatic
fun main(args: Array<String>) {
val processors = ArrayList<Processor>()
for (i in 0..999) {
val data = IntArray(10000000)
processors.add(object : Processor(data) {
override fun calculate(): Int {
return length
}
})
}
println(processors.stream().mapToLong { obj: Processor ->
obj.calculate()
.toLong()
}.sum())
}
}
internal abstract class Processor(data: IntArray) {
var length: Int
init {
length = data.size
}
abstract fun calculate(): Int
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment