|
| 1 | +package com.android.developers.androidify.util |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.graphics.Bitmap |
| 5 | +import android.os.Handler |
| 6 | +import android.os.Looper |
| 7 | +import android.webkit.WebView |
| 8 | +import java.io.File |
| 9 | +import java.io.RandomAccessFile |
| 10 | +import java.nio.ByteBuffer |
| 11 | +import java.nio.MappedByteBuffer |
| 12 | +import java.nio.channels.FileChannel |
| 13 | +import java.util.UUID |
| 14 | + |
| 15 | +object MemorySimulator { |
| 16 | + private val javaHeapLeaker = mutableListOf<String>() |
| 17 | + private val bitmapPool = mutableListOf<Bitmap>() |
| 18 | + private val directBuffers = mutableListOf<ByteBuffer>() |
| 19 | + private val mappedBuffers = mutableListOf<MappedByteBuffer>() |
| 20 | + private val webViews = mutableListOf<WebView>() |
| 21 | + private val mainHandler = Handler(Looper.getMainLooper()) |
| 22 | + |
| 23 | + /** |
| 24 | + * Toggles massive Java Heap string allocations. |
| 25 | + * Excellent for forcing the OS to swap compressed pages to zRAM (Swap). |
| 26 | + */ |
| 27 | + fun startJavaHeapSwapBloat(targetMegaBytes: Int, intervalMs: Long = 100) { |
| 28 | + val bytesPerAlloc = 1024 * 1024 // 1 MB increments |
| 29 | + val totalLoops = targetMegaBytes |
| 30 | + |
| 31 | + var allocated = 0 |
| 32 | + mainHandler.post(object : Runnable { |
| 33 | + override fun run() { |
| 34 | + if (allocated < totalLoops) { |
| 35 | + val stringChunk = StringBuilder().apply { |
| 36 | + repeat(bytesPerAlloc / 2) { append(UUID.randomUUID().toString().take(2)) } |
| 37 | + }.toString() |
| 38 | + |
| 39 | + javaHeapLeaker.add(stringChunk) |
| 40 | + allocated++ |
| 41 | + mainHandler.postDelayed(this, intervalMs) |
| 42 | + } |
| 43 | + } |
| 44 | + }) |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Toggles Native Bitmap Allocation (Anon RSS). |
| 49 | + * Directly inflates the private dirty native heap without exhausting JVM heap limits. |
| 50 | + */ |
| 51 | + fun startNativeBitmapBloat(targetMegaBytes: Int, intervalMs: Long = 200) { |
| 52 | + val width = 512 |
| 53 | + val height = 512 |
| 54 | + var allocated = 0 |
| 55 | + |
| 56 | + mainHandler.post(object : Runnable { |
| 57 | + override fun run() { |
| 58 | + if (allocated < targetMegaBytes) { |
| 59 | + val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) |
| 60 | + bitmap.eraseColor(android.graphics.Color.RED) |
| 61 | + bitmapPool.add(bitmap) |
| 62 | + allocated++ |
| 63 | + mainHandler.postDelayed(this, intervalMs) |
| 64 | + } |
| 65 | + } |
| 66 | + }) |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Toggles Direct Native Allocations (C/C++ malloc equivalent). |
| 71 | + * Bypasses the JVM heap completely and allocates raw native memory. |
| 72 | + */ |
| 73 | + fun startDirectNativeBloat(targetMegaBytes: Int, intervalMs: Long = 100) { |
| 74 | + var allocated = 0 |
| 75 | + mainHandler.post(object : Runnable { |
| 76 | + override fun run() { |
| 77 | + if (allocated < targetMegaBytes) { |
| 78 | + // Allocate 1MB of direct native memory |
| 79 | + val buffer = ByteBuffer.allocateDirect(1024 * 1024) |
| 80 | + // Touch the memory so the OS maps it to physical RAM |
| 81 | + for (i in 0 until buffer.capacity() step 4096) { |
| 82 | + buffer.put(i, 1) |
| 83 | + } |
| 84 | + directBuffers.add(buffer) |
| 85 | + allocated++ |
| 86 | + mainHandler.postDelayed(this, intervalMs) |
| 87 | + } |
| 88 | + } |
| 89 | + }) |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Toggles the WebView "Hidden Tab" trick. |
| 94 | + * Rapidly consumes massive amounts of memory via Chromium rendering processes. |
| 95 | + */ |
| 96 | + fun startWebViewBloat(context: Context, targetInstances: Int = 10, intervalMs: Long = 500) { |
| 97 | + var allocated = 0 |
| 98 | + mainHandler.post(object : Runnable { |
| 99 | + override fun run() { |
| 100 | + if (allocated < targetInstances) { |
| 101 | + val webView = WebView(context).apply { |
| 102 | + settings.javaScriptEnabled = true |
| 103 | + // Load a heavy WebGL/rendering page (or just a heavy DOM) |
| 104 | + loadUrl("https://threejs.org/examples/#webgl_animation_keyframes") |
| 105 | + } |
| 106 | + webViews.add(webView) |
| 107 | + allocated++ |
| 108 | + mainHandler.postDelayed(this, intervalMs) |
| 109 | + } |
| 110 | + } |
| 111 | + }) |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Memory maps massive files to inflate the virtual memory footprint. |
| 116 | + */ |
| 117 | + fun startFileMmapBloat(context: Context, targetMegaBytes: Int) { |
| 118 | + Thread { |
| 119 | + try { |
| 120 | + // Create a huge dummy file |
| 121 | + val dummyFile = File(context.cacheDir, "huge_dummy_file.tmp") |
| 122 | + val raf = RandomAccessFile(dummyFile, "rw") |
| 123 | + val targetBytes = targetMegaBytes * 1024L * 1024L |
| 124 | + raf.setLength(targetBytes) |
| 125 | + |
| 126 | + // mmap the file into memory |
| 127 | + val channel = raf.channel |
| 128 | + val mappedBuffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, targetBytes) |
| 129 | + |
| 130 | + // Touch the bytes to force physical allocation |
| 131 | + for (i in 0 until targetBytes step 4096) { |
| 132 | + mappedBuffer.put(i.toInt(), 1.toByte()) |
| 133 | + } |
| 134 | + |
| 135 | + mappedBuffers.add(mappedBuffer) |
| 136 | + } catch (e: Exception) { |
| 137 | + e.printStackTrace() |
| 138 | + } |
| 139 | + }.start() |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Clears all simulated allocations to allow the OS to reclaim space. |
| 144 | + */ |
| 145 | + fun reset() { |
| 146 | + mainHandler.removeCallbacksAndMessages(null) |
| 147 | + javaHeapLeaker.clear() |
| 148 | + |
| 149 | + bitmapPool.forEach { it.recycle() } |
| 150 | + bitmapPool.clear() |
| 151 | + |
| 152 | + directBuffers.clear() |
| 153 | + mappedBuffers.clear() |
| 154 | + |
| 155 | + webViews.forEach { |
| 156 | + it.stopLoading() |
| 157 | + it.destroy() |
| 158 | + } |
| 159 | + webViews.clear() |
| 160 | + |
| 161 | + System.gc() |
| 162 | + } |
| 163 | +} |
0 commit comments