• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Memory Not getting released

 
A Thakur
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Stanislav Lapitsky
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Art, please UseCodeTags next time.
 
A Thakur
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic