diff options
| author | Abdellah El Morabit <nsrddyn@gmail.com> | 2025-11-29 16:05:02 +0100 |
|---|---|---|
| committer | Abdellah El Morabit <nsrddyn@gmail.com> | 2025-11-29 16:05:02 +0100 |
| commit | 0c5e2d23133fcff99eab74662091af1a69f6084b (patch) | |
| tree | 3c12f46b4b03b262b4436f1b6ece51837d877fe9 /src/main/scala | |
| parent | ff2942a46d943f2ef30ac74ef6ea75992a3ac26c (diff) | |
feature: succesfully allocated memory on the off heap (about 1gb) assigned some value to the first 4 memory adress spaces
https://docs.oracle.com/en/java/javase/17/docs/api/jdk.incubator.foreign/jdk/incubator/foreign/MemorySegment.html
https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/lang/foreign/Arena.html
-> live savers
Diffstat (limited to 'src/main/scala')
| -rw-r--r-- | src/main/scala/main/Main.scala | 22 | ||||
| -rw-r--r-- | src/main/scala/main/domain/MemoryOperations.scala | 67 | ||||
| -rw-r--r-- | src/main/scala/main/infrastructure/Resources.scala | 3 |
3 files changed, 53 insertions, 39 deletions
diff --git a/src/main/scala/main/Main.scala b/src/main/scala/main/Main.scala index 1406fc0..6baa0ce 100644 --- a/src/main/scala/main/Main.scala +++ b/src/main/scala/main/Main.scala @@ -4,14 +4,24 @@ import main.domain._ import main.services._ import main.infrastructure._ import oshi._ -import zio._ +// import zio._ +import java.lang.foreign._ -object Torque extends ZIOAppDefault { +object Torque /* extends ZIOAppDefault */ { - def run: ZIO[ZIOAppArgs & Scope, Any, Any] = { - Console.printLine("=== TORQUE STRESS TEST ===") - - } + def main(args: Array[String]): Unit = MemoryAllocater.run() + // override def run: ZIO[ZIOAppArgs & Scope, Any, Any] = memoryExecution + // + // def memoryExecution: ZIO[Any, Throwable, Unit] = { + // + // + // for { + // _ <- ZIO.debug("started") + // f1 = MemoryAllocater.run() + // _ <- ZIO.debug("finished") + // + // } yield () + // } } diff --git a/src/main/scala/main/domain/MemoryOperations.scala b/src/main/scala/main/domain/MemoryOperations.scala index 3e45470..b58968f 100644 --- a/src/main/scala/main/domain/MemoryOperations.scala +++ b/src/main/scala/main/domain/MemoryOperations.scala @@ -1,7 +1,6 @@ package main.domain import main.services._ - import scala.util.hashing import scala.util.hashing.MurmurHash3 import scala.collection.immutable.ListSet @@ -11,44 +10,46 @@ import java.lang.foreign.MemorySegment import java.lang.foreign.ValueLayout import scala.util.Using -object Memory { - - - /** - * Large memory object to allocate on the heap - * This is an implementation by me - * A method to do this bassically an Array with large bytes allocated to it - * */ - - class MemoryObject { - - // TODO: create a large sized object - - } - /* - * Using java.nio.ByteBuffer - * Allocate memory then free memory - * for stressing the memory we want to do some heavy heap allocations back and forth - * --------- - * not anymore - * switched to java.lang.foreign - * - * https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/lang/foreign/package-summary.html - * */ - class MemoryAllocater(arena: Arena) { +/** + * Large memory object to allocate on the heap + * This is an implementation by me + * A method to do this bassically an Array with large bytes allocated to it + * */ +// class MemoryObject { +// +// // TODO: create a large sized object +// +// } - def run(): Unit = moveToMemory() +/* + * Using java.nio.ByteBuffer + * Allocate memory then free memory + * for stressing the memory we want to do some heavy heap allocations back and forth + * --------- + * not anymore + * switched to java.lang.foreign + * + * https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/lang/foreign/package-summary.html + * */ - def moveToMemory(): Unit = { +object MemoryAllocater { - val memseg = arena.allocate(10 * 4) - // not memseg.setAtIndex like in the docs :( - for i <- 0 to 10 do memseg.set(ValueLayout.JAVA_INT, i, i) + def run(): Unit = { - } + val arena: Arena = Arena.ofConfined() - } + // I think this should be around one 1Gb of data + val memorySegment = arena.allocate((10 * 1024 * 1024) * 100) + // not memseg.setAtIndex like in the docs :( + /** + * adding an offset so that the next piece of memory wont be overlapping with the one we just assigned + * */ + val offset: Int = 4 + for i <- 0 until 10 do memorySegment.set(ValueLayout.JAVA_INT, offset * i, i) + for i <- 0 until 10 do println("Index : " + memorySegment.get(ValueLayout.JAVA_INT, offset * i)) + + } } diff --git a/src/main/scala/main/infrastructure/Resources.scala b/src/main/scala/main/infrastructure/Resources.scala index f4e88c2..0915d3b 100644 --- a/src/main/scala/main/infrastructure/Resources.scala +++ b/src/main/scala/main/infrastructure/Resources.scala @@ -33,4 +33,7 @@ class Resources { // println("temperature: " + sensors.getCpuTemperature()) } } + + + // TODO: add method for viewing threads } |
