作者:漂亮的花裙子 | 来源:互联网 | 2023-09-11 10:55
WroteasimpleprogramtofillALLTHESPACEonmydrive.Ithasaverysimplelogic:写了一个简单的程序来填充我的
Wrote a simple program to fill ALL THE SPACE on my drive. It has a very simple logic:
写了一个简单的程序来填充我的驱动器上的所有空间。它有一个非常简单的逻辑:
Well, I just created a new Cocoa project from scratch and placed the code in the applicationDidFinishLaunching
. Here is it.
好吧,我刚从头创建了一个新的Cocoa项目,并将代码放在applicationDidFinishLaunching中。就这个。
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
fm = [NSFileManager new];
unsigned long long size = 0;
unsigned long long sizeMax = 0;
NSString *path;
NSData *data;
NSFileHandle *myHandle;
for (long i = 0; ; ++i) {
if (i % 40L == 0) { // N = 40
sizeMax = [self currentMaxSize];
size = 0;
path = [NSString stringWithFormat:@"/Users/user/Desktop/loltest/lol_%f.txt", [NSDate timeIntervalSinceReferenceDate]];
[fm createFileAtPath:path contents:[NSData data] attributes:nil];
[myHandle closeFile];
myHandle = [NSFileHandle fileHandleForWritingAtPath:path];
}
[myHandle seekToEndOfFile];
data = [self randomData];
size += data.length;
[myHandle writeData:data];
data = nil;
if (size >= sizeMax) {
[myHandle closeFile];
break;
}
}
NSLog(@"OKAY");
}
- (unsigned long long)currentMaxSize
{
return [[fm attributesOfFileSystemForPath:@"/" error:nil][NSFileSystemFreeSize] unsignedLongLongValue];
}
- (NSData *)randomData
{
const int LENGTH = 100 * 1024 * 1024; // M
char *bytes = (char *)malloc(LENGTH * sizeof(char)); // Well that supposed to be static and dispatched_once, but I removed that when tried to fix leaks
for (NSInteger i = 0; i
When I ran that one, after 1 minute 1 GiB leaked and still. Ended on the system's memory management window and activity monitor showing me 60 GiB of swap used.
当我跑了那个,1分钟后1 GiB泄露并且仍然。结束系统的内存管理窗口和活动监视器,显示我使用的60 GiB交换。
I ran the Instruments which said me that Overall bytes
section is equal to what the activity monitor shows for the app. However, there is only one object at a time, the allocation graph is a saw, the pointer of the random data is constant between function calls. Maybe NSData
somehow holds the source array, but I can't solve this puzzle at all.
我运行的仪器告诉我,整体字节部分等于活动监视器为应用程序显示的内容。但是,一次只有一个对象,分配图是一个锯,随机数据的指针在函数调用之间是恒定的。也许NSData以某种方式持有源数组,但我根本无法解决这个难题。
1 个解决方案