This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Memory and Time management Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Memory and Time management" Watch "Memory and Time management" New topic
Author

Memory and Time management

Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
In Java, we can program a Question in number of ways. But, which how to know the memory usage and time required by a particular program to execute?

I think, after knowing this, one can really improve his/her way of coding.

Thanks In Advance


kunal
Noel Alex
Greenhorn

Joined: Jul 25, 2011
Posts: 7


Well, a lot of tools exists to do this for you..

Inside your Java JDK there is a file called: jvisualvm which is found inside bin... Have you tried it??

Which king of problem are you facing exactly?
Is it what you want?

Goodluck!!


MWaNDuLu
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Thanks Noel for your reply

I am not having any problem, just wondering, how to improve my coding part. Because memory and time, both is IMPORTANT. And you can code a single program in number of ways. So, how to know which coding is efficient with respect to time consumed and memory.

I haven't tried jvisualvm.

Will you please assist me.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Study algorithms and data structures, so that you understand how much time an algorithm will take or how much memory a datastructure will take as the number of items to be processed or stored grows. See: Analysis of algorithms.

For something concrete, you can study different sorting algorithms, for example quicksort, bubble sort, insertion sort, etc. The question to answer is: if the collection to sort has N items, then how much time (on average) would it take to sort those items? Is it approximately N units of time, or N^2 units of time, or N log(N) units of time?

The book Introduction to Algorithms is a classic university-level book about learning to analyze algorithms and data structures.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Thanks Jesper de Jong for your concern

Sorry to say, but i didn't ever studied Data structures and Algorithms as i am from a commerce background.

I know, Data structures and Algorithms will help me a lot, but, is there any other tools provided for fetching memory usage and time consumed by the code?
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Someone please reply.

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11170
    
  16

first, you should EaseUp. everyone here is a volunteer, and gives what time they can.

There are plenty of tools that can help you - search for "java profilers". The problem is that they can only tell you how your EXISTING code runs, not how some theoretical code will run. You don't want to write out the code using one method, see how it runs, then develop it all over again using a new method and see how that runs in comparison. At least, I hope you don't.

Your other option is to learn about data structures and algorithms, and then do some data analysis. The classic example is sorting. There are perhaps a dozen different sort algorithms. Some work great when the data is mostly sorted and you are just sticking in a (relatively) few new data points, but do HORRIBLE if the data is completely random. Others are the exact opposite. This, then requires you to have an idea of what your data will look like, and what you will be doing.

there is no 'magic bullet'.

Finally, your BEST best it to document ahead of time what your requirements are - and they should be SPECIFIC. a spec of "it will run as fast as possible" is meaningless. It is ALWAYS possible to run faster, if you buy bigger/better/badder hardware, but that costs $$$. Write code that is neat, clean, easy to read, and easy to debug (since you will be doing a LOT of debugging). Once that is done, IF it doesn't meet your requirements, use a profiler to find out where you are slow, and work on that part of the code.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Thanks for your reply fred rosenberger.


Actually i never studied Data Structures & Algorithms as i am from a commerce(Accounts) background. But, in many interviews, they ask me these things.

Finally i am going to join a small company relating to web designing and development. And i will be the only programmer. So, i am really confused whether i should go for Data Structures & Algorithms or just stick to what i know.

What do you suggest fred rosenberger?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Kunal Lakhani wrote:Actually i never studied Data Structures & Algorithms as i am from a commerce(Accounts) background. But, in many interviews, they ask me these things.

The fact that you're from a commerce background should not be an excuse to not study data structures and algorithms.

Data structures and algorithms are fundamental building blocks for software engineers - there's a good reason why people ask you about these things in job interviews. If you want to become a serious software engineer, you're going to have to study them sooner or later. Because you'll need to know how to choose the right algorithms and data structures to write efficient programs.
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Will you please suggest a book for that?
Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
Jesper de Jong wrote:
The book Introduction to Algorithms is a classic university-level book about learning to analyze algorithms and data structures.


Hi kunal, you have been already suggested a book by jesper.
Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
Noel Alex wrote:
Well, a lot of tools exists to do this for you..

Inside your Java JDK there is a file called: jvisualvm which is found inside bin... Have you tried it??

Can you tell me how to use this file ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
webpage
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Rameshwar Soni wrote:Can you tell me how to use this file ?

In a Windows command prompt window, type the command "jvisualvm".
Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
Jesper de Jong wrote:
In a Windows command prompt window, type the command "jvisualvm".

When i tried i got the following
C:\>jvisualvm
'jvisualvm' is not recognized as an internal or external command, operable program or batch file.
C:\>

And yes i path is properly set because when i type javac i get a list of commands.
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
After typing the command "jvisualvm". in a Windows command prompt window?


I went through some documentation. But didnt understood how to use jvisualism.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
That is obviously a path problem, Rameshwar Soni. Open the java installation folder specified in your path environment variable, until you find the "bin" folder; jvisualvm should be inside that. If not, try reinstalling Java.
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

Hi Rameshwar Soni,

if I may give you some friendly advice.

The first one: Relax. Don't try to write the "best code ever written". Write code that can be easily understandable and maintained.
The second one: Don't overdo it. A program evolves. Do what you have to do, try it, test it, stress it and learn how to find bottlenecks. When you find them, sort them out by refactoring them and ask specific question to those who are elderly and should know better.
The third one: Relax. And then take up Jespers advice and study algorithms. They have nothing to do with the Java language in specific. They are more general and fit more or less into all languages.
The fourth and final one: Have fun! Play around. Try things. One thing you could do is trying to answer questions asked here at the Ranch. You might not be quickest but at least you will try for yourself and then, most likely, one of the elderly will have a good answer that you can study.
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Thanks all of you for valuable suggestions.

I will definitely consider the advice of going through algorithms.


Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
Ove Lindström wrote:Hi Rameshwar Soni,

if I may give you some friendly advice.

The first one: Relax. Don't try to write the "best code ever written". Write code that can be easily understandable and maintained.
The second one: Don't overdo it. A program evolves. Do what you have to do, try it, test it, stress it and learn how to find bottlenecks. When you find them, sort them out by refactoring them and ask specific question to those who are elderly and should know better.
The third one: Relax. And then take up Jespers advice and study algorithms. They have nothing to do with the Java language in specific. They are more general and fit more or less into all languages.
The fourth and final one: Have fun! Play around. Try things. One thing you could do is trying to answer questions asked here at the Ranch. You might not be quickest but at least you will try for yourself and then, most likely, one of the elderly will have a good answer that you can study.

Thanks Ove for your advice, they are always helpful for any beginner like me.
Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
Campbell Ritchie wrote:That is obviously a path problem, Rameshwar Soni. Open the java installation folder specified in your path environment variable, until you find the "bin" folder; jvisualvm should be inside that. If not, try reinstalling Java.

I will try it out. Thanks for your reply
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Memory and Time management
 
Similar Threads
two JVM under one Processor
� Is it possible to write a Java program that accumulates memory over time?
Garbage Collection
why my program throw the OutOfMemeory
Static variables and running the same class twice