You should not use the Files.readAllBytes class when converting the file to bytes. After using different byte converter. The main problem is that the byte array in the variable cannot be deleted from memory.
example 1
byte[] x1=new byte[50];
x1=null; //not memory deleting;
example 2
byte[] x2=null; //memory space:0
x2=new byte[50]; //memory space:50
x2=new byte[100]; //memory space:150
x2=new byte[10]; //memory space:160
x2=null; //memory space:160
I realized this was the situation.
how can we clear this from memory space? There were dynamic memory allocation methods in C language (Malloc), this memory is not deleted in java.
package dragdrop;
import java.awt.EventQueue;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.TransferHandler;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class gfgh {
private JFrame frame;
private File_Class file_Class=null;
private JPanel panel;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
gfgh window = new gfgh();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public gfgh() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setBackground(Color.BLACK);
frame.getContentPane().add(panel, BorderLayout.CENTER);
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
memorydelete();
}
});
panel.add(btnNewButton);
this.DragDrop(panel);
}
private void DragDrop(JComponent component)
{
@SuppressWarnings("serial")
TransferHandler transferHandler=new TransferHandler(){
@Override
public boolean canImport(JComponent component,DataFlavor[] dataFlavor)
{
return true;
}
@SuppressWarnings("unchecked")
@Override
public boolean importData(JComponent comp, Transferable t) {
// TODO Auto-generated method stub
List<File> files;
try {
files = (List<File>) t.getTransferData(DataFlavor.javaFileListFlavor);
for(File file:files)
{
System.out.println(file.getAbsolutePath());
System.out.println(file.length());
file_Class=new File_Class();
//file read examle 1 false
// file_Class.file=Files.readAllBytes(Paths.get(file.getAbsolutePath()));
//* file read example 2 false
/*
RandomAccessFile f = new RandomAccessFile(file.getAbsolutePath(), "r");
file_Class.file = new byte[(int)f.length()];
f.readFully( file_Class.file);
*/
//example 3 false
// file_Class.file=readFile(file);
//example 4 false
file_Class.file=readFile(file);
memorydelete();
///////
}
files=null;
Runtime.getRuntime().gc();
} catch (UnsupportedFlavorException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
};
component.setTransferHandler(transferHandler);
}
public void memorydelete()
{
file_Class.file=null;
file_Class=null;
Runtime.getRuntime().gc();
}
public static byte[] readFile(File file) throws IOException {
// Open file
RandomAccessFile f = new RandomAccessFile(file, "r");
try {
// Get and check length
long longlength = f.length();
int length = (int) longlength;
if (length != longlength)
throw new IOException("File size >= 2 GB");
// Read file and return data
byte[] data = new byte[length];
f.readFully(data);
return data;
} finally {
f.close();
}
}
}
class File_Class {
public byte file[];
}