wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Memory Not getting released Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Memory Not getting released" Watch "Memory Not getting released" New topic
Author

Memory Not getting released

A Thakur
Greenhorn

Joined: Apr 06, 2010
Posts: 20
I am component where browsing the directory and select a file.
After selecting the file, I load the file into JTextArea using following code


private void loadDocument(String fileName){
try {
System.out.println("Starting Loading "+fileName);
File flType = new File(fileName);
FileReader fr = new FileReader(flType);
BufferedReader br = new BufferedReader(fr);
getTextArea().read(br, null);

fr.close();
br.close();

System.out.println("Done Loading");
}
catch (Exception e) {
e.printStackTrace();
}
}

When I load a big document (~40MB- 100MB), I see memory utilization to 500MB on task Manager.

But if I open another document (let say a smaller size document), it does not release the memory. And keep on increasing memory utilization from 500MB to goes on to 1GB and beyond.

I understand that JTextArea component is memory consuming but real problem is when it should release memory as I select and load another document.

I even tried setting text of textarea to empty string before loading new document by getTextArea().setText("")... It does not help in releasing memory.

Any suggestions to release memory will help.

Thanks.


SCJP 5.0
Stanislav Lapitsky
Ranch Hand

Joined: Dec 01, 2009
Posts: 53
Did you try to call System.gc() after opening a new document or calling .setText("")?

Memory is not released immediately after sme object can be destroyed but when gc is called.

If it doesn't help please post here a minimal example to illustrate the problem.

Regards,
Stas
A Thakur
Greenhorn

Joined: Apr 06, 2010
Posts: 20
Yes, I have tried calling System.gc() before loading each document. Also tried calling getTextArea.setText("") but none of these helped in getting memory released.

Below are some of components:

// JText Area to display the file

private JTextArea getTextArea(){
if(_txtArea == null){
_txtArea = new JTextArea();
_txtArea.setMinimumSize(new Dimension(120, 80));
_txtArea.setMaximumSize(new Dimension(120, 80));
_txtArea.setEditable(false);
_txtArea.setWrapStyleWord(true);
_txtArea.setLineWrap(true);
_txtArea.setFont(new Font("Aerial", Font.PLAIN, 11));
}
return _txtArea;
}

// Button to browse

private JButton getOpenButton(){
if(_btnOpen == null){
_btnOpen = new JButton("Open");
ActionListener openDirectionActionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
openFileChooser();
}
};

_btnOpen.addActionListener(openDirectionActionListener);
}
return _btnOpen;
}

// Open Button to action

private void openFileChooser(){
if(_fileChooser == null){
_fileChooser = new JFileChooser();
}


_fileChooser.setMultiSelectionEnabled(false);
int option = _fileChooser.showOpenDialog(SampleJTextArea.this);
if (option == JFileChooser.APPROVE_OPTION) {
try{
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
getTextArea().setText("");
File selectedFile = _fileChooser.getSelectedFile();

loadDocument(selectedFile.getAbsolutePath());
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
catch(Exception ex){
ex.printStackTrace();
}
}
}

// load document in TextArea

private void loadDocument(String fileName){
try {
System.out.println("Starting Loading "+fileName);
File flType = new File(fileName);
FileReader fr = new FileReader(flType);
BufferedReader br = new BufferedReader(fr);
pane.read(br, null);


fr.close();
br.close();

System.out.println("Done Loading");
}
catch (Exception e) {
e.printStackTrace();
}
}


I would really appreciate your help.

Thanks a bunch
Stanislav Lapitsky
Ranch Hand

Joined: Dec 01, 2009
Posts: 53
I don't see any problems with the code.
You can also try to eliminate the old document by
txtArea .setDocument(new PlainDocument());

could be the old documnet somehow keeps references.

Also check whether you have any listeners related to the tetx area and/or document.

Regards,
Stas
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Art, please UseCodeTags next time.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
A Thakur
Greenhorn

Joined: Apr 06, 2010
Posts: 20
I did tried to use setting plain document.



This did not release memory as well. Although I saw he memory finally releasing after 10 mins of no activity on GUI. Seems like its holding the big string/ document for a while.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Memory Not getting released