Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 23rd, 2005, 10:51 PM   #1
B3TA_SCR1PT3R
Hobbyist Programmer
 
B3TA_SCR1PT3R's Avatar
 
Join Date: Jul 2005
Location: Dallas, Texas
Posts: 101
Rep Power: 0 B3TA_SCR1PT3R is an unknown quantity at this point
Send a message via AIM to B3TA_SCR1PT3R
Reduce Memory Consumption

Hey i was wondering if there was a command for reducing the memory consumption, or CPU usage of a java program.
I dont know if this is typical of java programs, but im pushing around 20,000K just writing text to a file.
is this normal?, is it just my computer?, or something wrong in the code maybe?

heres some of the code: (its the only loop)
BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
Random rand = new Random();
for (int line = 0; line <= MAX; line++)
		{
			for (int num = 0; num <= MAX; num++)
			{
				intValue = rand.nextInt(90) + (10);
				bw.write(intValue + " ");
			}
			bw.newLine();
		}
__________________
Hoes telling me to calm down but I'm like fuck that shit!
B3TA_SCR1PT3R is offline   Reply With Quote
Old Dec 23rd, 2005, 11:19 PM   #2
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 310
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
It's not pushing 20,000K because of you're writing to a text file. It's pushing 20,000K because of your nested for loops.
andro is offline   Reply With Quote
Old Dec 23rd, 2005, 11:38 PM   #3
B3TA_SCR1PT3R
Hobbyist Programmer
 
B3TA_SCR1PT3R's Avatar
 
Join Date: Jul 2005
Location: Dallas, Texas
Posts: 101
Rep Power: 0 B3TA_SCR1PT3R is an unknown quantity at this point
Send a message via AIM to B3TA_SCR1PT3R
well i kinda need a nested for loop to make a square type output...is there anyway to reduce the memory consumption?
__________________
Hoes telling me to calm down but I'm like fuck that shit!
B3TA_SCR1PT3R is offline   Reply With Quote
Old Dec 23rd, 2005, 11:45 PM   #4
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
I believe that this is normal because Java programs run on Java virtual machine instead of running natively on windows. Not sure though.

BTW, for loops don't fill up memory, they use up CPU power.
OpenLoop is offline   Reply With Quote
Old Dec 24th, 2005, 1:28 AM   #5
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
The memory usage isn't directly related to the nested loop (except for the fact that you're writing (MAX+1)*(MAX+1) integer values with separating whitespace to a file).

The memory usage is probably related to the fact that you're using a BufferedWriter. A BufferedWriter stores data (i.e. buffers it) in memory until it is flushed (i.e. physically written) to the file. That buffering can consume memory.

Writing directly to the FileWriter will probably eliminate the buffering (and hence the memory usage), but the trade-off will be I/O performance of your program. The disk (or other I/O device) is usually an I/O bottleneck for any program, and slowing that I/O down tends to have a very noticeable effect on how fast a file is written. A rough rule of thumb is that physically writing lots of little chunks to disk will be slower than writing a few big chunks. The whole purpose of buffered I/O is using memory to store data temporarily until bigger chunks are available to be written. The amount of memory consumed depends on how the buffering is implemented but most good-quality schemes will work within available memory resources. I'm not sure offhand how good the scheme used by BufferedWriter is.
grumpy is offline   Reply With Quote
Old Dec 24th, 2005, 4:57 AM   #6
Klipt
Hobbyist Programmer
 
Join Date: Dec 2005
Posts: 118
Rep Power: 0 Klipt is an unknown quantity at this point
I would assume 20 000k is well within 'available resources'
Klipt is offline   Reply With Quote
Old Dec 25th, 2005, 1:39 PM   #7
JavaMan
Newbie
 
Join Date: Oct 2005
Posts: 29
Rep Power: 0 JavaMan is on a distinguished road
I don't know if this would help, but you should close the reader after you are done using it.
JavaMan is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:25 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC