双向缓存的内存使用含义

时间:2015-08-13 07:51:28

标签: java pointers caching memory

我想知道以下代码段的内存使用含义。我的理解是每个实例中只有一个实例存储在内存中,第二个缓存中的每一个实例都只是创建一个指针。如果创建指针消耗了多少空间?有没有办法监视JVM中的内容和内存使用情况,以下示例?是否有必要实习uid?对以下任何建议都表示赞赏。

- (void) doLogin:(NSDictionary *)dict
{
        TransactionsViewController *transactions;
        BalancesViewController *balances;
        ProfileViewController *profile;
        UINavigationController *navi;
        UITabBarController *root;

        root = (UITabBarController *)self.window.rootViewController;

        navi = [root.viewControllers objectAtIndex:0];
        transactions = [navi.viewControllers objectAtIndex:0];
        transactions.delegate = self;

        navi = [root.viewControllers objectAtIndex:1];
        balances = [navi.viewControllers objectAtIndex:0];
        balances.delegate = self;

        navi = [root.viewControllers objectAtIndex:2];
        profile = [navi.viewControllers objectAtIndex:0];
        profile.delegate = self;

        [transactions loadDataFromLogin:dict];
        [balances loadDataFromLogin:dict];
        [profile loadDataFromLogin:dict];
}

- (void) showLoginView
{
        assert(loginController == nil);
        assert(activityView == nil);

        UITabBarController *tabbar = (UITabBarController *)self.window.rootViewController;
        loginController = [tabbar.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
        loginController.delegate = self;
        [tabbar presentModalViewController:loginController animated:YES];
}

- (void) loginWithLogin:(NSDictionary *)dict relogin:(BOOL)relogin
{
        NSString *_login = [dict valueForKey:@"email"];
        NSString *_pass = [dict valueForKey:@"pass"];
        NetworkOperation *op = [NetworkOperation operationLogin:_login pass:_pass];
        [NetworkOperation enqueueOperation:op observer:self];
        if (!relogin && !loginController)
                [self doLogin:dict];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
        session = nil;
        loginController = nil;

        return YES;
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
        NSString *login = [[NSUserDefaults standardUserDefaults] stringForKey:@"email"];
        NSString *pass = [[NSUserDefaults standardUserDefaults] stringForKey:@"pass"];
        NSString *name = [[NSUserDefaults standardUserDefaults] stringForKey:@"name"];
        if ((login == nil || login.length == 0) ||
            (pass == nil || pass.length == 0) ||
            (name == nil || name.length == 0)) {
                if (!loginController) {
                        [self.window makeKeyAndVisible];
                        [self performSelector:@selector(showLoginView) withObject:nil afterDelay:0.1];
                }
        } else {
                NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:login, @"email",
                                      pass, @"pass", name, @"name", nil];
                [self loginWithLogin:dict relogin:(session != nil)];
        }
}

2 个答案:

答案 0 :(得分:1)

对象引用在64位JVM上最多需要8个字节,但由于CompressedOops 4个字节。由于您只有100000个对象,因此地图引用将占用4 * 4 * 100000 = 1,6MB的内存。地图内容将占用100,000 *(sizeof(UUID)+ sizeof(Long)),这仍然是几十兆字节。

Interning帮助使用相同的字符串,U中的UUID之一告诉您为什么这对您没有帮助。不是说你真的需要帮助。一张100k条目的小地图不值得特别关注。

答案 1 :(得分:0)

您可以考虑使用双向Map API,而不是自己维护两张地图。例如,请查看BiMap提供的Guava界面。使用BiMap的优点是:它将保留其值及其键的唯一性。

对于cache1,您只需执行bimap.get(key)即可。对于cache2,您执行bimap.inverse().get(key)