Posts: 266
Threads: 239
Joined: Apr 2011
A.A. Sir! Thanks for replying. You have told me the following line of code to "java –Xms10m –Xmx250m MyApp" to set memory taken from heap. Please tell me how and where I can use in a Java project in NetBeans that on running project it automatically allocates enough memory and don't crash. Thank you.
Posts: 9,583
Threads: 8,089
Joined: Apr 2011
In Netbean , select the project , right click and select Properties.
In properties select Run
And add –Xmx256m in VM options. This will allocate 256 MB for your program.
You can get the information regarding the Heap Memory using following code lines.
// Get current size of heap in bytes
long heapSize = Runtime.getRuntime().totalMemory();
// Get maximum size of heap in bytes. The heap cannot grow beyond this size.
// Any attempt will result in an OutOfMemoryException.
long heapMaxSize = Runtime.getRuntime().maxMemory();
// Get amount of free memory within the heap in bytes. This size will increase
// after garbage collection and decrease as new objects are created.
long heapFreeSize = Runtime.getRuntime().freeMemory();
Note: You save some of your objects on disk and then use them back when needed. This way your code will be using less memo