System.gc();
System.out.println((Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())/1024/1024);// print 1M
Map map=new HashMap();
for(int i=0;i<100000;i++){
map.put("key"+i,"i");
}
System.gc();
System.out.println((Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())/1024/1024); //print 10M
map.clear();
System.gc();
System.out.println((Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory()));//print less than 1M
Run Code Online (Sandbox Code Playgroud)
看来调用clear方法时内存会减少。然而,从其他答案来看,该clear方法似乎从未缩小HashMap. 那么为什么内存会减少呢?
有三个viewController,MainViewController ViewControllerB和ViewControllerC。
MainViewController将在应用程序启动时加载。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
MainViewController * main = [[MainViewController alloc]init];
UINavigationController * navigationController = [[UINavigationController alloc]initWithRootViewController:main];
self.window.rootViewController = navigationController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
return YES;
}
Run Code Online (Sandbox Code Playgroud)
并且有一个button关于MainViewController,现在ViewControllerB,
UIViewController *rootViewController = [[UIApplication sharedApplication].keyWindow rootViewController];
ViewControllerB * vcb=[[ViewControllerB alloc] init];
[rootViewController presentViewController:vcb animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)
在之后 …