简体   繁体   English

如何释放类实例和内存?

[英]How to release class instances and memory?

I am using JProfiler to troubleshoot my swing based desktop application for memory issues. 我正在使用JProfiler对基于swing的桌面应用程序进行内存故障排除。 In Memory view of JProfiler, I see 40K instances of a class and using 3MB of memory . 在JProfiler的“内存”视图中,我看到一个类的40K 实例 ,并使用3MB的内存

DefaultTableModel summaryModel = null;

void insertEnv(final Map<String, String> configMap,final String variable,final String value)
{
    this.configMap = configMap;

    Runnable runnable = new Runnable() 
    {
        public void run() 
        {               
            Vector dataVector  = summaryModel.getDataVector();

            for(int row = 0; dataVector != null && row < dataVector.size(); row++)
            {
                Vector rowData = (Vector)dataVector.get(row);
                if(rowData.get(0).toString().equals(variable))
                {
                    summaryModel.removeRow(row);
                }
            }

            Object[] row = new Object[] {variable, value };
            summaryModel.addRow(row);               
        }
    };

    SwingUtilities.invokeLater(runnable);
}

In insertEnv() function, I am adding new row in a JTable. 在insertEnv()函数中,我正在JTable中添加新行。 This function is called 40K times and thus 40K instances of above class. 此函数被调用40K次,因此被称为40K以上类的实例。 What can be done to release the memory and instance count? 如何释放内存和实例数?

Are You sure properly close everything engine need to close? 您确定正确关闭了引擎需要关闭的所有物品吗?

You write "add from event new row to JTable" which seems to be bad pattern, but code use kind of model, can bee good. 您编写“从事件新行添加到JTable中”似乎是错误的模式,但是代码使用某种模型可以很好。

JTable with very big data sets can/should be used in "virtual" mode. 可以/应该在“虚拟”模式下使用具有很大数据集的JTable。 Look at google. 看谷歌。 4MB of RAM usually isn't a problem, but collecting garbage 40k "pieces of objects" may be hard :( 4MB的RAM通常不是问题,但是收集40k垃圾“件”可能很难:(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM