Skip to content

Instantly share code, notes, and snippets.

@wkgcass
Last active September 24, 2023 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wkgcass/60124ca51cfcc868cc475bd6f27da12f to your computer and use it in GitHub Desktop.
Save wkgcass/60124ca51cfcc868cc475bd6f27da12f to your computer and use it in GitHub Desktop.
test mmap rss
public class Main {
public static void main(String[] args) {
var arr = new byte[(1024 * 1024)][];
new Thread(() -> {
while (true) {
var total = Runtime.getRuntime().totalMemory();
System.out.println(total);
try {
Thread.sleep(5 * 1000);
} catch (Exception ignore) {
}
}
}).start();
while (true) {
for (int i = 0; i < 1024 * 1024; ++i) {
arr[i] = new byte[400];
}
for (int i = 0; i < 1024 * 1024; ++i) {
arr[i] = null;
}
}
}
}
#!/bin/bash
set -e
javac Main.java
java -XX:+UseZGC -Xmx2G -verbose:gc Main
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <linux/memfd.h>
#include <fcntl.h>
#include <string.h>
#include <inttypes.h>
int main() {
int fd = memfd_create("mytest", MFD_CLOEXEC);
printf("fd = %d\n", fd);
int err = ftruncate(fd, 1073741824);
if (err) {
perror("ftruncate failed");
return 1;
}
err = fallocate(fd, 0, 0, 0x33400000);
if (err) {
perror("fallocate failed");
return 1;
}
void* mmap_res_1 = mmap((void*)0x100000000, 0x33400000, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, 0);
if (mmap_res_1 == NULL) {
perror("mmap 1 failed");
return 1;
}
printf("mmap_res_1 = %lu\n", (size_t) mmap_res_1);
memset(mmap_res_1, 1, 0x33400000);
for (int i = 0; i < 64; ++i) {
printf("iteration = %d\n", i);
uint64_t loc = (2L + i) << (4 * 8);
void* mmap_res_2 = mmap((void*)loc, 0x33400000, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, 0);
if (mmap_res_2 == NULL) {
perror("mmap 2 failed");
return 1;
}
printf("mmap_res_2 = %lu\n", (size_t) mmap_res_2);
printf("mmap_res_2[last] = %d\n", ((char*)mmap_res_2)[ 0x33400000 - 1 ]);
for (uint64_t j = 0; j < 0x33400000; ++j) {
char v = ((char*)mmap_res_2)[j];
if (v != (i + 1)) {
printf("invalid value at %lu = %d, expecting %d\n", j, v, (i + 1));
break;
}
}
memset(mmap_res_2, i + 2, 0x33400000);
}
sleep(120);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment