gpt4 book ai didi

java - 从容器的实例中删除组件是否保留在内存中?

转载 作者:行者123 更新时间:2023-11-29 06:04:23 25 4
gpt4 key购买 nike

我有一个容器,可以在几个屏幕大小的 Pane 之间切换其主要内容。我没有使用 CardLayout,而是使用 remove(previous);添加(当前);验证();

在这个容器中,我有每个 Pane 的数据字段,它们在启动时被初始化,这样我就可以轻松地在它们之间切换引用。

我的问题:
如果移除之前的pane并添加新的/当前的pane,那么之前pane的实例对象占用的内存是否还在内存中?

因为我考虑过将前一个 Pane 设置为 null 并在将其添加到容器之前重新创建当前 Pane 以尝试降低内存使用量,但不确定它是否真的会产生任何影响。

谢谢。 :)

编辑:这实际上不是我的课,但它演示了我将如何切换 View :

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();
}
}

最佳答案

If you remove the previous pane and add the new / current one, does the memory taken up by the previous pane's instance object remain in memory?

是的,除非您删除对该对象的所有 引用。完成此操作后,它将有资格进行垃圾回收。

Because I considered setting the previous pane to null...

好主意!

关于java - 从容器的实例中删除组件是否保留在内存中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9050602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com