Memory usage improvements:
- Use smaller number types int -> short
    saves in heap size.
    - Save UID of vehicles as int (4 Bytes)
    or short (2 Bytes) and when needed, make
    a String as UID. A string would take up
    (16 Bytes).
			
			
This commit is contained in:
		
							parent
							
								
									5e1ff6451c
								
							
						
					
					
						commit
						69c1a19ed9
					
				
							
								
								
									
										10
									
								
								src/Bus.java
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								src/Bus.java
									
									
									
									
									
								
							@ -1,11 +1,11 @@
 | 
			
		||||
 | 
			
		||||
public class Bus extends CivilVehicle {
 | 
			
		||||
	
 | 
			
		||||
	private String UID;
 | 
			
		||||
	private int UID;
 | 
			
		||||
	private Campaign campaign;
 | 
			
		||||
	private static int numeberOfBuses;
 | 
			
		||||
    private final int TIME_TO_FIX = 20; //in minutes
 | 
			
		||||
    public static final int MAX_FORWARD = 900; //Meter/Min
 | 
			
		||||
    private final short TIME_TO_FIX = 20; //in minutes
 | 
			
		||||
    public static final short MAX_FORWARD = 900; //Meter/Min
 | 
			
		||||
 | 
			
		||||
    public static final double STD_BUS_SIZE = 10;
 | 
			
		||||
 | 
			
		||||
@ -40,11 +40,11 @@ public class Bus extends CivilVehicle {
 | 
			
		||||
 | 
			
		||||
    private void generateUID() {
 | 
			
		||||
        numeberOfBuses++;
 | 
			
		||||
        this.UID = String.format("BUS%04d", numeberOfBuses);
 | 
			
		||||
        this.UID = numeberOfBuses;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public String getUID(){
 | 
			
		||||
        return this.UID;
 | 
			
		||||
        return String.format("BUS%04d", UID);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Campaign getCampaign() {
 | 
			
		||||
 | 
			
		||||
@ -467,7 +467,6 @@ public class GUI_History {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public String avgTimeOfTrip() {
 | 
			
		||||
		//TODO: does output diff value even after all have arrived.
 | 
			
		||||
		Calendar now = new GregorianCalendar();
 | 
			
		||||
		now.setTime(currenttimeManager);
 | 
			
		||||
		Calendar from = (GregorianCalendar)now.clone();
 | 
			
		||||
 | 
			
		||||
@ -251,7 +251,7 @@ public class GUI_ViewStreet {
 | 
			
		||||
		if (vehicles.isEmpty()) return;
 | 
			
		||||
		vehicleData = new Object[vehicles.size()][6];
 | 
			
		||||
		for (int i = 0; i < vehicles.size(); i++) {
 | 
			
		||||
			vehicleData[i][0] = vehicles.get(i).getUID();// TODO: There is an Exception error here;
 | 
			
		||||
			vehicleData[i][0] = vehicles.get(i).getUID();
 | 
			
		||||
			if (vehicles.get(i) instanceof Bus)
 | 
			
		||||
				vehicleData[i][1] = ((Bus)vehicles.get(i)).getCampaign().getHotelDistrict().name();
 | 
			
		||||
			else vehicleData[i][1] = "Local Vehicle";
 | 
			
		||||
 | 
			
		||||
@ -56,7 +56,6 @@ public class MakkahCity {
 | 
			
		||||
 | 
			
		||||
	public static void main(String[] args) {
 | 
			
		||||
 | 
			
		||||
		//TODO: fix state errors
 | 
			
		||||
		t.start();
 | 
			
		||||
		//Gen Camp
 | 
			
		||||
		campPerDistrict[District.ALMANSOOR.ordinal()] = new ArrayList<>();
 | 
			
		||||
@ -592,7 +591,7 @@ public class MakkahCity {
 | 
			
		||||
						stdStreet[i].getPercentRemainingCapacity());
 | 
			
		||||
			}
 | 
			
		||||
			String input = in.next();
 | 
			
		||||
			int index = Integer.parseInt(input);//TODO: unhandled ex
 | 
			
		||||
			int index = Integer.parseInt(input);
 | 
			
		||||
			showStreet(stdStreet[index]);
 | 
			
		||||
		}
 | 
			
		||||
		if (choice.equals("4")){
 | 
			
		||||
@ -911,7 +910,7 @@ public class MakkahCity {
 | 
			
		||||
			if (street.getName() == StreetNames.IBRAHIM_ALKHALIL2) numOfSedan = (int) (numOfSedan * 1.2);
 | 
			
		||||
			for (int x = 0; x < numOfSedan; x++) {
 | 
			
		||||
				Sedan car = new Sedan(getRandom(4, 5));
 | 
			
		||||
				double pointOfEntry = getRandom(0, street.getLength());//TODO: consider getLength - x
 | 
			
		||||
				double pointOfEntry = getRandom(0, street.getLength());
 | 
			
		||||
				if (street.capcityPoint(pointOfEntry, pointOfEntry+1500) < 1){
 | 
			
		||||
					listOfVehicles.add(car);
 | 
			
		||||
					car.setCurrentLocation(pointOfEntry);
 | 
			
		||||
@ -924,7 +923,7 @@ public class MakkahCity {
 | 
			
		||||
			if (street.getName() == StreetNames.FOURTH_HIGHWAY1) numOfTruck = (int) (numOfTruck * 0.5);
 | 
			
		||||
			if (street.getName() == StreetNames.FOURTH_HIGHWAY2) numOfTruck = (int) (numOfTruck * 0.5);
 | 
			
		||||
			if (street.getName() == StreetNames.STREET3) numOfTruck = (int) (numOfTruck * 1.5);
 | 
			
		||||
			if (street.getName() == StreetNames.IBRAHIM_ALKHALIL2) numOfSedan = (int) (numOfSedan * 1.2);
 | 
			
		||||
			if (street.getName() == StreetNames.IBRAHIM_ALKHALIL2) numOfTruck = (int) (numOfTruck * 1.2);
 | 
			
		||||
			for (int x = 0; x < numOfTruck; x++) {
 | 
			
		||||
				Truck car = new Truck(getRandom(4, 5));
 | 
			
		||||
				double pointOfEntry = getRandom(0, street.getLength());
 | 
			
		||||
@ -939,7 +938,7 @@ public class MakkahCity {
 | 
			
		||||
			if (street.getName() == StreetNames.FOURTH_HIGHWAY1) numOfSUV = (int) (numOfSUV * 0.5);
 | 
			
		||||
			if (street.getName() == StreetNames.FOURTH_HIGHWAY2) numOfSUV = (int) (numOfSUV * 0.5);
 | 
			
		||||
			if (street.getName() == StreetNames.STREET3) numOfSUV = (int) (numOfSUV * 1.5);
 | 
			
		||||
			if (street.getName() == StreetNames.IBRAHIM_ALKHALIL2) numOfSUV = (int) (numOfSedan * 1.2);
 | 
			
		||||
			if (street.getName() == StreetNames.IBRAHIM_ALKHALIL2) numOfSUV = (int) (numOfSUV * 1.2);
 | 
			
		||||
			for (int x = 0; x < numOfSUV; x++) {
 | 
			
		||||
				SUV car = new SUV(getRandom(4, 5));
 | 
			
		||||
				double pointOfEntry = getRandom(0, street.getLength());
 | 
			
		||||
@ -950,19 +949,12 @@ public class MakkahCity {
 | 
			
		||||
					car.setCurrentStreet(street);
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public static PDate getTimeMan() {
 | 
			
		||||
		return currenttimeManager;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Find shortest path without respect to traffic
 | 
			
		||||
	 * @param campaign
 | 
			
		||||
	 * @return
 | 
			
		||||
	 */
 | 
			
		||||
	
 | 
			
		||||
	private static Route getBestRoute(Campaign campaign , Mashier mashier) {
 | 
			
		||||
		//ArrayList<Route> routes = (ArrayList<Route>) Arrays.asList(getRoutesToDistrict(campaign.getHotelDistrict()));
 | 
			
		||||
@ -998,7 +990,12 @@ public class MakkahCity {
 | 
			
		||||
		}
 | 
			
		||||
		return sortingRoute;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Find shortest path without respect to traffic
 | 
			
		||||
	 * @param campaign
 | 
			
		||||
	 * @return
 | 
			
		||||
	 */
 | 
			
		||||
	public static Route getShortestRoute(Campaign campaign, Mashier mashier) {
 | 
			
		||||
		Route[] routes = getRoutesToDistrict(campaign.getHotelDistrict());
 | 
			
		||||
		Route route = null;
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										13
									
								
								src/SUV.java
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								src/SUV.java
									
									
									
									
									
								
							@ -1,9 +1,9 @@
 | 
			
		||||
 | 
			
		||||
public class SUV extends CivilVehicle {
 | 
			
		||||
	private String UID;
 | 
			
		||||
	private static int numeberOfSUV;
 | 
			
		||||
    private final int TIME_TO_FIX = 15; //in minutes
 | 
			
		||||
    public static final int MAX_FORWARD = 1300;
 | 
			
		||||
	private short UID;
 | 
			
		||||
	private static short numeberOfSUV;
 | 
			
		||||
    private final short TIME_TO_FIX = 15; //in minutes
 | 
			
		||||
    public static final short MAX_FORWARD = 1300;
 | 
			
		||||
 | 
			
		||||
    public SUV(double vehicleSize){
 | 
			
		||||
        super(vehicleSize);
 | 
			
		||||
@ -21,12 +21,11 @@ public class SUV extends CivilVehicle {
 | 
			
		||||
    
 | 
			
		||||
    private void generateUID() {
 | 
			
		||||
    	numeberOfSUV++;
 | 
			
		||||
    	this.UID = String.format("SUV%04d", numeberOfSUV);
 | 
			
		||||
    	
 | 
			
		||||
    	this.UID = numeberOfSUV;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public String getUID(){
 | 
			
		||||
        return this.UID;
 | 
			
		||||
        return String.format("SUV%04d", UID);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,10 +1,10 @@
 | 
			
		||||
 | 
			
		||||
public class Sedan extends CivilVehicle {
 | 
			
		||||
 | 
			
		||||
	private String UID;
 | 
			
		||||
	private static int numeberOfSedan;
 | 
			
		||||
    private final int TIME_TO_FIX = 15; //in minutes
 | 
			
		||||
    public static final int MAX_FORWARD = 1500; // Meter/Min
 | 
			
		||||
	private short UID;
 | 
			
		||||
	private static short numeberOfSedan;
 | 
			
		||||
    private final short TIME_TO_FIX = 15; //in minutes
 | 
			
		||||
    public static final short MAX_FORWARD = 1500; // Meter/Min
 | 
			
		||||
 | 
			
		||||
    public Sedan(double vehicleSize){
 | 
			
		||||
        super(vehicleSize);
 | 
			
		||||
@ -16,12 +16,11 @@ public class Sedan extends CivilVehicle {
 | 
			
		||||
    
 | 
			
		||||
    private void generateUID() {
 | 
			
		||||
    	numeberOfSedan++;
 | 
			
		||||
    	this.UID = String.format("Sedan%04d", numeberOfSedan);
 | 
			
		||||
    	
 | 
			
		||||
    	this.UID = numeberOfSedan;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public String getUID(){
 | 
			
		||||
        return this.UID;
 | 
			
		||||
        return String.format("Sedan%04d", UID);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
 | 
			
		||||
@ -1,8 +1,8 @@
 | 
			
		||||
 | 
			
		||||
public class Truck extends CivilVehicle {
 | 
			
		||||
	private String UID;
 | 
			
		||||
	private static int numeberOfTruck;
 | 
			
		||||
    private final int TIME_TO_FIX = 20; //in minutes
 | 
			
		||||
	private short UID;
 | 
			
		||||
	private static short numeberOfTruck;
 | 
			
		||||
    private final short TIME_TO_FIX = 20; //in minutes
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public int getMaxSpeed() {
 | 
			
		||||
@ -19,12 +19,13 @@ public class Truck extends CivilVehicle {
 | 
			
		||||
    }
 | 
			
		||||
    private void generateUID() {
 | 
			
		||||
    	numeberOfTruck++;
 | 
			
		||||
    	this.UID = String.format("Truck%04d", numeberOfTruck);
 | 
			
		||||
    	this.UID = numeberOfTruck;
 | 
			
		||||
    }
 | 
			
		||||
    		  
 | 
			
		||||
    	    public String getUID(){
 | 
			
		||||
    	        return this.UID;
 | 
			
		||||
    	    }
 | 
			
		||||
    	}
 | 
			
		||||
 | 
			
		||||
    public String getUID(){
 | 
			
		||||
        return String.format("Truck%04d", UID);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user