Solved

Memory allocation in java programatically

Posted on 2006-06-22
14
364 Views
Last Modified: 2012-08-14
Hi All,

Any clues on how to allocate memory in java at runtime?
I mean suppose in my main method if I want to say that "this program should run with memory of 512MB" is there any way programatically
We use command like

java -Xms512M -Xmx512M abc

Is there anyway to implement it in your java program?

Thanks.

Rgds,
Arpit
0
Comment
Question by:arpit080399
  • 5
  • 4
  • 2
  • +2
14 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
No
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
no, you need to do it from the command line as you already are.
0
 

Author Comment

by:arpit080399
Comment Utility
ok let me explain the problem I have suppose you guys can give me a better solution

I am facing problem while parsing large XML file. filesize is around 35MB and it gives me error of "outofMemoryError" while executing DocumentBuilder.parse command.

now the problem is this particular program is a part of sequence on a larger program, hence cannot be executed standalone as indicated above.
Any ideas on how to tackle this kind of memory allocation errors?

Thanks
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
if it is ruunninmg in the same vm as the larger program then you need to specify the required memory size when you start that program.
You wouldn't need to change mx, just mx.

If its in a different vm , then specify the size when you start the vm
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Can you use SAX? - if so it will save a lot of resources
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
You should use the SAX parser for files which are big in size and cannot be loaded in memory at one instant - that is what it is meant for and traditionally that is one of its advantages over DOM (and this is one of DOM's disadvantages).
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
>> You wouldn't need to change mx, just mx.

:-) ?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:arpit080399
Comment Utility
SAX cannot be used as we have to modify and manipulate the XML file.
ok, so only while starting the program we need to allocate the mx parameter .
Is there any other better way? because i require such high memory only for this one program and dont want to allocate this memory forever as some other applications are also running on this machine.
large program is a timer, hence will always be executed after every 30 mins
0
 
LVL 92

Accepted Solution

by:
objects earned 125 total points
Comment Utility
> >> You wouldn't need to change mx, just mx.

should have been:

>> You wouldn't need to change mx, just ms.

mx only specified the max required, it will only get allocated when needed.
0
 

Author Comment

by:arpit080399
Comment Utility
ok objects, you mean that
I should increase my ms parameter to the value i require and mx will be allocated by JVM?
does it mean that if i say
java -Xms2056M abc then 2056MB will always be reserved by JVM whether it is being utilised or no?
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
no, I mean increase mx to the most you need.
and set ms to the amount your app will typically need.
0
 
LVL 1

Expert Comment

by:sharath_kulal
Comment Utility
You could use JDOM.
JDOM uses best of DOM and SAX. JDOM supports document modification with less footprint(i.e. doesn't load the whole document in memory)
http://www.javaworld.com/javaworld/jw-05-2000/jw-0518-jdom.html

hope this helps
0
 

Author Comment

by:arpit080399
Comment Utility
Thanks objects, Infact what i want to ask is
that suppose if i give command like
java -Xms 1024M -Xmx 2048M abc
then how much memory will JVM always reserve for this program? will it always keep 1024 or 2048 reserved?
I mean to say how does JVM treat ms and mx parameters?
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
ms specifies the initial size
mx the maximum size

http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/java.html
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now