Big memory usage spike

So I’ve been working on my tile map editor and I’ve been having some issues with massive memory usage spikes when you attempt to save the map

Here’s the code I have to save the world to a file. Hopefully this should all make sense:


	public static void requestWorldSave(World w) throws FileNotFoundException {
		JFrame frame = new JFrame();
		frame.setLocation(0, 0);
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		frame.setLayout(null);
		frame.pack();

		Dimension d = new Dimension(400, 400);

		frame.setPreferredSize(d);
		frame.setMaximumSize(d);
		frame.setMinimumSize(d);
		
		JFileChooser jfc = new JFileChooser();
		int r = jfc.showSaveDialog(frame);
		if (r == JFileChooser.APPROVE_OPTION) {
			File f = jfc.getSelectedFile();
			String contents = "";
			
			WorldInfo wi = w.getInfo();
			
//Loop through all the tiles in the world
			for (int x = 0; x < wi.nChunksX * Chunk.chunkW; x++) {
				for (int y = 0; y < wi.nChunksY * Chunk.chunkH; y++) {
					contents += w.getTile(x, y).getID() + " ";
				}
			}
			
			contents = FileEncrypter.encryptString(contents);
			
			PrintWriter pr = new PrintWriter(f);
			pr.write(contents);
			pr.close();
			
//			f = null;
//			contents = "";
//			contents = null;
//			pr = null;
//			jfc = null;
//			
//			System.gc();
		}
	}

You’ll see the commented out lines of code was what i tried to reduce to memory usage but it didn’t seem to work.

The memory spikes from around 40mb to 400mb, i admit 400mb is manageable and not a great deal but I’d at least like to get to the bottom of why this is happening.

Thanks :slight_smile: