Results 1 to 3 of 3
Thread: Huge memory allocation
- 12-08-2011, 12:38 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 1
- Rep Power
- 0
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:
PLEASE, can anyone help my to solve this problem?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(); } }
Thanks for any replies.
- 12-08-2011, 02:06 AM #2
Re: Huge memory allocation
Get a bigger computer or store fewer bytes in the one that you have.
- 12-08-2011, 01:06 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
Memory Allocation
By nathey in forum New To JavaReplies: 1Last Post: 07-18-2011, 03:48 AM -
Arrays memory allocation
By maya700 in forum New To JavaReplies: 5Last Post: 07-16-2011, 08:47 AM -
JNI Memory Allocation
By Smokin' Caterpillar in forum Advanced JavaReplies: 3Last Post: 09-16-2010, 05:00 PM -
Memory Allocation
By zzpprk in forum Advanced JavaReplies: 2Last Post: 03-16-2010, 01:14 AM -
Memory Allocation
By kishan in forum New To JavaReplies: 3Last Post: 09-19-2009, 05:47 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks