Results 1 to 3 of 3
  1. #1
    mctommek is offline Member
    Join Date
    Dec 2011
    Posts
    1
    Rep Power
    0

    Default Huge memory allocation

    Hello,
    I'm developing an application for image processing purposes. My app used to process many images (e.g. 300) with a spatial resolution of 512x512px each. Every pixel in the image is stored as Point object and every image is stored in the Point's array - Point[][]. Following images are stored in an ArrayList<Point[][]>, so that the total size of this ArrayList may be 300, for instance.

    The problem is that the memory allocation for reading such amount of data is up to 1,5 GB witch is definitely to much...

    Here is the code of my sample test class:

    Java Code:
    import java.util.ArrayList;
    import java.util.Hashtable;
    import java.util.Scanner;
    
    public class ObjectPerformanceTest {
    	public static void main(String[] args) {
    		Test test = new Test();
    	}
    }
    
    class Test {
    	
    	public Test() {
    		int count = 20; //INCREASE THIS VALUE (e.g. 200) AND OBSERVE MEMORY USAGE
    		
    		test1(count);
    		System.out.println("-------------");
    		test2(count);
    		
    		Scanner scanner = new Scanner(System.in);
    		scanner.nextLine();
    	}
    	
    	private void test1(int count) {
    		float time = 0.0F;
    		
    		time = performCreateByNewTest(count);
    		
    		System.out.println("New object consuming: " + time + " sec");
    		displayMemory();
    	}
    	
    	private void test2(int count) {
    		try {
    			float time = 0.0F;
    			
    			time = performCreateByCloneTest(count);
    			
    			System.out.println("Clone object consuming: " + time + " sec");
    			displayMemory();
    		} catch (CloneNotSupportedException e) {
    			
    		}
    	}
    
    	private float performCreateByNewTest(int count) {
    		long start = System.currentTimeMillis();
    		
    		ArrayList<Point[][]> pointsArr = new ArrayList<Point[][]>();
    		for (int c = 0; c < count; c++) {
    			Point[][] points = new Point[512][512];
    			
    			for (int i = 0; i < 512; i++)
    				for (int j = 0; j < 512; j++) {
    					Point p = new Point(i, j);
    					p.setIntensity(1000);
    					points[i][j] = p;
    				}
    			
    			pointsArr.add(points);
    		}
    		
    		long elapsedTimeMillis = System.currentTimeMillis()-start;
    		return elapsedTimeMillis/1000F;
    	}
    	
    	private float performCreateByCloneTest(int count) throws CloneNotSupportedException {
    		long start = System.currentTimeMillis();
    		
    		ArrayList<Point[][]> pointsArr = new ArrayList<Point[][]>();
    		for (int c = 0; c < count; c++) {
    			Point[][] points = new Point[512][512];
    			
    			for (int i = 0; i < 512; i++)
    				for (int j = 0; j < 512; j++) {
    					Point p = createPointObject();
    					p.setX(i);
    					p.setY(j);
    					p.setIntensity(1000);
    					points[i][j] = p;
    				}
    			
    			pointsArr.add(points);
    		}
    		
    		long elapsedTimeMillis = System.currentTimeMillis()-start;
    		return elapsedTimeMillis/1000F;
    	}
    	
    	Hashtable<String, Point> _cacheTemplate = new Hashtable<String, Point>();
    	public Point createPointObject() throws CloneNotSupportedException {
    		Point p = (Point) _cacheTemplate.get( "Point" );
    		if (p == null) {
    			p = new Point();
    			_cacheTemplate.put( "Point", p );
    		}
    		return (Point) p.clone();
    	}
    	
    	public void displayMemory() {
    		int mb = 1024*1024;
    	    Runtime r = freeMemory();
    	    System.out.println("Total memory: " + (double)r.totalMemory()/mb + " MB");
    	    System.out.println("Free memory: " + (double)r.freeMemory()/mb + " MB");
    	    System.out.println("Memory Used="+(double)(r.totalMemory()-r.freeMemory())/mb + " MB");
    	}
    	
    	public Runtime freeMemory() {
    		Runtime r = Runtime.getRuntime();
    	    r.gc();
    	    r.gc();
    	    return r;
    	}
    	
    }
    
    class Point implements Cloneable, Comparable<Point> {
    	private int x = 0;
    	private int y = 0;
    	private int intensity = 0;
    	
    	public Point() {}
    	
    	public Point(int x, int y) {
    		this.x = x;
    		this.y = y;
    	}
    	
    	public int getX() {
    		return x;
    	}
    	public void setX(int x) {
    		this.x = x;
    	}
    	public int getY() {
    		return y;
    	}
    	public void setY(int y) {
    		this.y = y;
    	}
    	public int getIntensity() {
    		return intensity;
    	}
    	public void setIntensity(int intensity) {
    		this.intensity = intensity;
    	}
    	
    	public void increase(int val){
    		this.intensity = this.intensity + val;
    	}
    	
    	@Override
    	public String toString() {
    		return "("+x+", "+y+") = "+intensity;
    	}
    	
    	public Object clone() throws CloneNotSupportedException {
    		Object obj = super.clone();
    		return obj;
        }
    	
    	public int compareTo(Point p) {
    		return this.getIntensity() - p.getIntensity();
    	}
    }
    PLEASE, can anyone help my to solve this problem?

    Thanks for any replies.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Huge memory allocation

    Get a bigger computer or store fewer bytes in the one that you have.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default Re: Huge memory allocation

    Each image (ie Point[][] object) is 3Meg at a minimum (3 ints times 512 * 512 bytes)...so how do you expect to reduce the memory footprint?

    As Norm says, get more memory or don't store so much.

Similar Threads

  1. Memory Allocation
    By nathey in forum New To Java
    Replies: 1
    Last Post: 07-18-2011, 03:48 AM
  2. Arrays memory allocation
    By maya700 in forum New To Java
    Replies: 5
    Last Post: 07-16-2011, 08:47 AM
  3. JNI Memory Allocation
    By Smokin' Caterpillar in forum Advanced Java
    Replies: 3
    Last Post: 09-16-2010, 05:00 PM
  4. Memory Allocation
    By zzpprk in forum Advanced Java
    Replies: 2
    Last Post: 03-16-2010, 01:14 AM
  5. Memory Allocation
    By kishan in forum New To Java
    Replies: 3
    Last Post: 09-19-2009, 05:47 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •