0

I have an application to show a image of the user's data. Where each user has more than 1 image data. When I select the user in the JTable, my image data show in JPanel. The picture actually also JPanel with background image.

My question is, memory consumption always increases, and not reduce. Sometimes the application hangs. How to register the picture(JPanel) to garbage collection. And if it possible, when i must to register them? I am new in performance issue in Java.

This is my code :

public void getStreamData(final PanelEntry view, final String data) {

        String files = null;
        String path = null;

        if(isImages()) {
            path = "data/"+data+"/images";
        } else {
            path = "data/"+data+"/videos/thumbs";
        }

        File folder = new File(path);

        // Always remove previous image label when new data selected
        view.getPanelStream().removeAll();

        if (!folder.exists()) {
            JLabel label = new JLabel("No Stream Data");
            label.setForeground(Color.red);
            label.setVisible(true);

            // Adding to panelGallery
            view.getPanelStream().add(label);
            view.getPanelStream().revalidate();
            view.getPanelStream().repaint();

        } else {
            File [] listOfFiles = folder.listFiles();

            int maxFiles = listOfFiles.length;
            int maxView  = 15;

            // Loop for get image from file
            for (int i = listOfFiles.length; i > 0 ; i--) {
                if(listOfFiles[i].isFile()) {
                    files = listOfFiles[i].getName();
                    final String videoFiles = files;

                    if(files.endsWith(".jpg") || files.endsWith(".JPG") || 
                            files.endsWith(".jpeg") || files.endsWith(".JPEG") ||
                                files.endsWith(".png") || files.endsWith(".PNG")) {
                        final String newPath = path+"/"+files;

                        try {
                            File showFile = new File(newPath);
                            ImageIcon imgSource = new ImageIcon(newPath);

                            JPanel labelGallery = new BackgroundImageRounded(showFile);
                            labelGallery.setLayout(null);
                            labelGallery.setPreferredSize(new Dimension(160, 120));
                            labelGallery.setVisible(true);

                            JLabel labelName = new JLabel(files);
                            labelName.setSize(150,15);
                            labelName.setLocation(8, 8);
                            labelName.setVisible(true);
                            labelName.setForeground(Color.ORANGE);
                            labelGallery.add(labelName);

                            String videoPath = "data/"+data+"/videos/";
                            String video    = videoFiles.replace(".jpg", ".wmv");
                            String videoFile = video.replace("thumb_", "video_");
                            final String videoPlayer = videoPath+videoFile;

                            if (isImages()) {
                                labelGallery.setToolTipText("View Image");
                            } else {
                                labelGallery.setToolTipText("Play Video");

                                JLabel iconPlayer = new JLabel();
                                iconPlayer.setIcon(new ImageIcon(getClass().getResource("/com/ikbiz/gastroscope/resources/player.png")));
                                iconPlayer.setSize(61,42);
                                iconPlayer.setVisible(true);
                                iconPlayer.setLocation(50, 35);
                                labelGallery.add(iconPlayer);


                            }

                            labelGallery.addMouseListener(new MouseAdapter() {
                                @Override
                                public void mouseClicked(MouseEvent e) {        
                                    if(isImages()) {
                                        ImageViewer viewer = new ImageViewer(newPath);
                                        viewer.setVisible(true);
                                    } else {
                                        VideoViewer videoViewer = new VideoViewer();
                                        videoViewer.setViewer(videoPlayer);


                                        videoViewer.setLocationRelativeTo(null);
                                        videoViewer.pack();
                                        videoViewer.setVisible(true);

                                    }
                                }
                            });

                            // Adding to panelGallery
                            view.getPanelStream().add(labelGallery);
                            view.getPanelStream().revalidate();
                            view.getPanelStream().repaint();

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

Please help,

Thank you.

fanjavaid
  • 1,676
  • 8
  • 34
  • 65
  • 2
    Objects will become eligible for garbage collection when there a non longer any strong reference's to them. The code your using doesn't "seem" to create any significant strong references that I can see, but you'd be better running a profiler across it – MadProgrammer Jul 16 '13 at 05:42
  • How do you measure the memory consumption? A profile or at least `jconsole` should give you some hints where the memory is consumed. – Uwe Plonus Jul 16 '13 at 05:56
  • I just view memory consume in Task Manager, every load images data memory always increase +- 13MB. I just remove using removeAll() and i already read, removeAll() not affect to Garbage Collection. – fanjavaid Jul 16 '13 at 07:29
  • unrelated: a) don't do any manual sizing/locating, instead use a suitable LayoutManager b) [don't use setXXSize, ever](http://stackoverflow.com/a/7229519/203657) you'll wreck any internal calculations of sizing hints – kleopatra Jul 16 '13 at 07:31

1 Answers1

0

You should use a memory profiler to find out what is going on.

Here, you talk about registering the JPanel, but how are you 100% sure that this is the kind of objects that consume memory ?

I think you should :

  1. Profile your heap utilization
  2. Find out the objects that consume a lot of memory
  3. Find where they are instantiated
  4. Find out why they are not reclaimed by the GC (what are the GC roots that links to them)

Without more information, you are just guessing that some portion of your code must be the culprit, but you do not have a clue.

Pierre Laporte
  • 1,205
  • 8
  • 11