Results 1 to 11 of 11
Thread: Java not using all free memory.
- 09-12-2010, 11:17 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 6
- Rep Power
- 0
Java not using all free memory.
Hello,
So can someone explain why I see this behaviour?
givesJava Code:Runtime runtime = Runtime.getRuntime(); System.out.println("free memory: " + runtime.freeMemory()); byte[] a = new byte[4000000]; System.out.println("free memory: " + runtime.freeMemory());
(9854904-5854888 gives 4000016)Java Code:free memory: 9854904 free memory: 5854888
givesJava Code:Runtime runtime = Runtime.getRuntime(); System.out.println("free memory: " + runtime.freeMemory()); int[] a = new int[1000000]; System.out.println("free memory: " + runtime.freeMemory());
(9854904-5854888 gives 4000016 (exact same as before))Java Code:free memory: 9854904 free memory: 5854888
givesJava Code:Runtime runtime = Runtime.getRuntime(); System.out.println("free memory: " + runtime.freeMemory()); byte[] a = new byte[4000000]; int[] b = new int[1000000]; System.out.println("free memory: " + runtime.freeMemory());
What on earth is going on?Java Code:free memory: 9854904 Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at ***.***.***.***.***.***.mergetest.main(mergetest.java:35)
Surely allocating those two arrays should only take up 8000032 bytes, well within the 9854904 java reports as having free...
- 09-13-2010, 06:04 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
First of all, do you know how the memory works in a computer?
- 09-13-2010, 09:25 AM #3
Member
- Join Date
- Sep 2010
- Posts
- 6
- Rep Power
- 0
Surely the Java should hide anything about how memory works in a computer from me?
Am I being stupid here?
- 09-13-2010, 09:40 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
- 09-13-2010, 09:54 AM #5
Member
- Join Date
- Sep 2010
- Posts
- 6
- Rep Power
- 0
I doubt it.
givesJava Code:Runtime runtime = Runtime.getRuntime(); System.out.println("free memory: " + runtime.freeMemory()); int[] a = new int[1000000]; System.out.println("free memory: " + runtime.freeMemory()); System.out.println("free memory: " + runtime.freeMemory()); System.out.println("free memory: " + runtime.freeMemory()); System.out.println("free memory: " + runtime.freeMemory()); System.out.println("free memory: " + runtime.freeMemory()); System.out.println("free memory: " + runtime.freeMemory()); System.out.println("free memory: " + runtime.freeMemory()); System.out.println("free memory: " + runtime.freeMemory()); System.out.println("free memory: " + runtime.freeMemory()); System.out.println("free memory: " + runtime.freeMemory());
Which I also find surprising since I would thing the print statement itself would leave some rubbish on the heap that wouldn't get garbage collected yet.Java Code:free memory: 9854904 free memory: 5854888 free memory: 5854888 free memory: 5854888 free memory: 5854888 free memory: 5854888 free memory: 5854888 free memory: 5854888 free memory: 5854888 free memory: 5854888 free memory: 5854888
- 09-13-2010, 10:39 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
I've just run the second one (that fails with you), with -Xmx12M:
free memory: 8192808
free memory: 3141880
Which gives 5050928, which is a strange number, considering the individual ones are 4000024.
All I can say is, you're on the edge of your memory there so the JVM might be doing strange things. With larger memory than I gave there I got the expected 8000048 with the two arrays.
- 09-13-2010, 10:57 AM #7
Member
- Join Date
- Sep 2010
- Posts
- 6
- Rep Power
- 0
So is there a way of knowing exactly how much memory I can use without Java freaking out?
I could maybe hack something like.
long availiable_mem = r.freeMemory()-1000000;
I couldn't find any documentation anywhere about how much of the Java free memory you can run into.
- 09-13-2010, 11:04 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Don't worry about it.
You either need the memory or you don't, don't micro manage it in Java.
What are you doing such that you feel you need to do this?
- 09-13-2010, 11:10 AM #9
Member
- Join Date
- Sep 2010
- Posts
- 6
- Rep Power
- 0
An external sort, with the VM limited to 10M
It's an assignment, I think it's set in java just because the people who set it feel like having fun.
I want to load the biggest chunk from disk that I can without running into an OutOfMemoryException
- 09-13-2010, 11:17 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
OK.
Don't push the boundary, because the JVM does more in the background than you can monitor. This sort of thing might make sense in C, but is pretty silly in Java, even on a mobile.
- 09-13-2010, 11:21 AM #11
Member
- Join Date
- Sep 2010
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
free memory of bufferedimage problem
By mr_empty in forum Java 2DReplies: 2Last Post: 01-17-2010, 06:27 PM -
Free JAVA IDE - JCODER
By samnav in forum Other IDEsReplies: 1Last Post: 08-02-2009, 04:59 PM -
how do I increase memory allocated to code cache (Non Heap Memory)
By manibhat in forum Advanced JavaReplies: 2Last Post: 08-21-2008, 07:33 PM -
How Can I get free memory ?
By sathish_2111 in forum NetworkingReplies: 2Last Post: 07-19-2007, 04:29 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks