Jump to content
  • Advertisement
Sign in to follow this  
arkmaster

Java Memory Usage spinning out of control

This topic is 4419 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all, I've been programming part of a project that performs simulations in Java lately, and one of the things we've implemented lately is a feature to save the state of the simulation at various points in time, due to the amount of time it takes to run any given simulation. I have replaced a Vector object in the engine with a new class I made that is intended to reduce memory usage by swapping out portions to disk. However, it seems that with this new class, if I try to load a state file of the engine, the resumed simulation will be using a lot more memory than if I had let the engine run continuously..this was not a problem before replacing the Vector class. I figure it may have something to do with the object serialization/deserialization code but I'm not sure what.., so I'm going to go ahead and post that here. However, if anyone knows any general reasons why object deserialization in Java can eat up a lot of memory, please let me know. EDIT: If anyone thinks seeing the rest of class's code would help, please let me know.
	private void writeObject(ObjectOutputStream out) throws IOException
	{
		//save statics
		out.defaultWriteObject();
		out.writeInt(globalInstanceCount);
		out.writeObject(tmpPath);
		//load all the pma files we created and write them as well
		for (long i = (totalRemovedSize/ elementsInMemory) + 2; i <= ((totalAddedSize-1)/elementsInMemory) - 1; i++) {
			String filename = tmpPath + "arrayInst" + currentInstanceCount + "_tmpNum" + i + ".pma";
			out.writeObject(readArrayFromFile(filename));
		}
			
	}
	
	private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
	{
		in.defaultReadObject();
		//restore statics
		globalInstanceCount = in.readInt();
		tmpPath = (String)in.readObject();
		for (long i = (totalRemovedSize/ elementsInMemory) + 2; i <= ((totalAddedSize-1)/elementsInMemory) - 1; i++) {
			String filename = tmpPath + "arrayInst" + currentInstanceCount + "_tmpNum" + i + ".pma";
			//PacketArray writeArray = (PacketArray)in.readObject();
			//writeArrayToFile(writeArray, filename);
			writeArrayToFile((PacketArray)in.readObject(), filename);
		}
	}
	
	private PacketArray  readArrayFromFile(String filename) {
		PacketArray returnArray = null;
		
		try {
			FileInputStream in = new FileInputStream(filename);
			ObjectInputStream s = new ObjectInputStream(in);
			returnArray = (PacketArray)s.readObject();
		} catch (FileNotFoundException e)	{
			System.err.println("FileNotFoundException: " + e.getMessage());
			System.err.println("Cannot load file, aborting...");
			System.exit(0);
		} catch (IOException e) {
			System.err.println("IOException: " + e.getMessage());
			System.err.println("Aborting...");
			System.exit(0);
		} catch (ClassNotFoundException e) {
			System.err.println("ClassNotFoundException: " + e.getMessage());
			System.err.println("Aborting...");
			System.exit(0);
	  	}
		if (returnArray == null) {
			System.err.println("Null array...this shouldn't happen.");
		}
		return returnArray;
	}
	
	private void  writeArrayToFile(PacketArray packetArray, String filename) {
		try {
			FileOutputStream out = new FileOutputStream(filename);
			ObjectOutputStream s = new ObjectOutputStream(out);
			s.writeObject(packetArray);
			s.flush();
			s.close();
			out.close();
		} catch (FileNotFoundException e) {
			System.err.println("FileNotFoundException: " + e.getMessage());
		} catch (IOException e) {
			System.err.println("IOException: " + e.getMessage());
		}
	}
[Edited by - arkmaster on June 13, 2006 9:57:16 AM]

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!