我有一个容器,它在几个屏幕大小的窗格之间切换其主要内容。我没有使用CardLayout
,而不是我使用remove(previous); add(current); validate();
从容器的实例中删除组件是否保留在内存中?
在这个容器我对这些窗格,这是在启动时初始化,这样我可以轻松地只需切换它们之间的参考的数据字段。
我的问题:
如果您删除以前的窗格中,并添加新的/当前,也由以前的窗格中的实例对象所占用的内存留在记忆?
因为我考虑设置一个窗格设置为null,以试图降低内存使用它添加到容器之前重新创建当前窗格,但不知道它实际上做任何区别。
谢谢。 :)
编辑:这不是我的实际类,但它表明我要如何如何切换的观点:
public class ViewManager {
public static final int VIEW_LOGIN = 0;
public static final int VIEW_CALENDAR = 1;
public static final int VIEW_HELP = 2;
public static final int VIEW_SETTINGS = 3;
public static final int VIEW_PREFERENCES = 4;
public static final int VIEW_STATS = 5;
private static LoginPane login = new LoginPane();
private static CalendarView calendar = new CalendarView();
private static HelpPane help = new HelpPane();
private static SettingsPane accountSettings = new SettingsPane();
private static PreferencesPane preferences = new PreferencesPane();
private static StatsPane stats = new StatsPane();
private static int previousView;
private static Object [] views = {login, calendar, help, accountSettings, preferences, stats};
// Without settings old views to null and re-creating incoming view request
public static void switchTo(int currentView){
if(currentView == previousView) return;
MainFrame.getContent().remove(views[previousView]);
MainFrame.getContent().add(views[currentView]);
MainFrame.getContent().validate();
}
// Settings to null and re-creating incoming view request
public static void switchToNullify(int currentView){
if(currentView == previousView) return;
MainFrame.getContent().remove(views[previousView]);
views[previousView] = null;
if(currentView == VIEW_LOGIN) views[VIEW_LOGIN] = new LoginPane();
else if(currentView == VIEW_CALENDAR) views[VIEW_CALENDAR] = new CalendarView();
else if(currentView == VIEW_HELP) views[VIEW_HELP] = new HelpPane();
else if(currentView == VIEW_SETTINGS) views[VIEW_ACCOUNT_SETTINGS] = new SettingsPane();
else if(currentView == VIEW_PREFERENCES) views[VIEW_PREFERENCES] = new PreferencesPane();
else if(currentView == VIEW_STATS) views[VIEW_STATS] = new StatsPane();
MainFrame.getContent().add(views[currentView]);
MainFrame.getContent().validate();
}
}
顺便说一句,这个问题的范围超出的Java Swing。这真的适用于OOP作为一个整体(即[消除过时的对象引用(http://my.safaribooksonline.com/9780137150021/ch02lev1sec6))。 – mre 2012-01-29 03:20:41