关于内存限制1MB
2026-04-08 22:57:17
发布于:上海
tough@localhost:/mnt/c/Users/nansh/Downloads$ g++ -O2 -nostdlib -nostartfiles -static test.cpp -o test
tough@localhost:/mnt/c/Users/nansh/Downloads$ ./test
7tough@localhost:/mnt/c/Users/nansh/Downloads$ smem -p -P test
PID User Command Swap USS PSS RSS
1949 tough /usr/bin/python3 /usr/bin/s 0.00% 0.09% 0.12% 0.18%
tough@localhost:/mnt/c/Users/nansh/Downloads$ perf stat ./test
7
Performance counter stats for './test':
0 context-switches:u # 0.0 cs/sec cs_per_second
0 cpu-migrations:u # 0.0 migrations/sec migrations_per_second
1 page-faults:u # 2278.4 faults/sec page_faults_per_second
0.44 msec task-clock:u # 0.0 CPUs CPUs_utilized
3 branch-misses:u # 100.0 % branch_miss_rate
3 branches:u # 0.0 M/sec branch_frequency
1,564 cpu-cycles:u # 0.0 GHz cycles_frequency
12 instructions:u # 0.0 instructions insn_per_cycle
<not counted> stalled-cycles-frontend:u # nan frontend_cycles_idle (0.00%)
0.003985969 seconds time elapsed
0.000000000 seconds user
0.001766000 seconds sys
tough@localhost:/mnt/c/Users/nansh/Downloads$
Ubuntu 24.04 LTS 内核上内存使用小于1MB
我的代码:
#pragma GCC optimize("O2")
#pragma GCC optimize("Os")
#pragma GCC optimize("no-exceptions")
asm(".global _start\n"
"_start:\n"
"sub $8, %rsp\n"
"movb $0x37, (%rsp)\n"
"mov $1, %rax\n"
"mov $1, %rdi\n"
"mov %rsp, %rsi\n"
"mov $1, %edx\n"
"syscall\n"
"add $8, %rsp\n"
"mov $60, %rax\n"
"xor %rdi, %rdi\n"
"syscall\n"
);
由于ACGO上无法使用-nostdlib -nostartfiles -static,我会把代码改成有主函数的版本:
#pragma GCC optimize("O2")
#pragma GCC optimize("Os")
#pragma GCC optimize("no-exceptions")
int main() {
asm volatile (
"sub $8, %%rsp\n"
"movb $0x37, (%%rsp)\n"
"mov $1, %%rax\n"
"mov $1, %%rdi\n"
"mov %%rsp, %%rsi\n"
"mov $1, %%edx\n"
"syscall\n"
"add $8, %%rsp\n"
"mov $60, %%rax\n"
"xor %%rdi, %%rdi\n"
"syscall\n"
::: "rax","rdi","rsi","rdx","rsp"
);
}
测评结果:

ACGO兼容版的测试:
tough@localhost:/mnt/c/Users/nansh/Downloads$ smem -p -P test
PID User Command Swap USS PSS RSS
2165 tough /usr/bin/python3 /usr/bin/s 0.00% 0.09% 0.12% 0.18%
tough@localhost:/mnt/c/Users/nansh/Downloads$ perf stat ./test
7
Performance counter stats for './test':
0 context-switches:u # 0.0 cs/sec cs_per_second
0 cpu-migrations:u # 0.0 migrations/sec migrations_per_second
1 page-faults:u # 2155.8 faults/sec page_faults_per_second
0.46 msec task-clock:u # 0.0 CPUs CPUs_utilized
3 branch-misses:u # 100.0 % branch_miss_rate
3 branches:u # 0.0 M/sec branch_frequency
2,810 cpu-cycles:u # 0.0 GHz cycles_frequency
12 instructions:u # 0.0 instructions insn_per_cycle
<not counted> stalled-cycles-frontend:u # nan frontend_cycles_idle (0.00%)
0.003652769 seconds time elapsed
0.000000000 seconds user
0.001615000 seconds sys
tough@localhost:/mnt/c/Users/nansh/Downloads$
原因:ACGO的内存统计还算上了其他进程(比如checker)的内存消耗
所以其实内存消耗<1MB是可以实现的,但是在OJ上做不到
各位大神可以试一试......
这里空空如也





















有帮助,赞一个