2012-01-29 33 views
1

我有一个容器,它在几个屏幕大小的窗格之间切换其主要内容。我没有使用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(); 
    } 
} 
+1

顺便说一句,这个问题的范围超出的Java Swing。这真的适用于OOP作为一个整体(即[消除过时的对象引用(http://my.safaribooksonline.com/9780137150021/ch02lev1sec6))。 – mre 2012-01-29 03:20:41

回答

3

如果您删除以前的窗格中,并添加新的/当前,也由以前的窗格中的实例对象采取 内存高达留在 内存?

是的,除非您删除所有对该对象的引用。一旦你完成了,它将有资格进行垃圾收集。

因为我认为前一个窗格设置为null ...

好主意!

+3

参见[Java程序中处理内存泄漏(http://www.ibm.com/developerworks/library/j-leaks/)。 – trashgod 2012-01-29 03:27:35

+0

将它们设置为null可确保GC,但这意味着稍后我将切换回它时,必须重新创建该对象。除非你删除对象的所有引用,否则请你详细说明一下吗? – rtheunissen 2012-01-29 03:36:44

+0

@偏执 - 安卓,如果重现以前的面板是昂贵的,那么我想你只需要缓存,但它会占用空间(即内存)。通过该评论,我的意思是除了对象本身之外的任何对象(对象,例如地图,列表,另一个类)都必须将引用设置为null,以便允许对象符合GC的条件。 – mre 2012-01-29 04:09:25

相关问题