We were designing an android app for a competition. I was trying to improvise on the features by trying to have a temporary cache on the app. I was going to use HashMap for this purpose. However, I was wondering if there is any special way of creating dynamic hashMap. If yes, please let me know as I am a java noob.(Google did not help much) As of now, i am using the normal declaration for hashMap.(I'm sure that's not right though).
JVM takes care of memory once an application as been terminated. Memory will automatically be free'd up after you exit the program.
Now, if you want to dump the hash map to a file, that's a different story, since HashMap implements Serializable, you can dump the object bytes to a file before the app exits, and load it back in when it starts back up.
Decent write up on Serialization:
http://java.sun.com/developer/technicalArticles/Programming/serialization/
Related
I need to read data from the mic input in realtime in my Java program. I experimented with TaroDSP a bit and while it does provide a fairly straightforward API for this, its latency is too high for my purposes.
I was thinking that maybe I can implement this in Max/DSP, write a program that would simply read the mic input levels, export it as a jar and include it in my java program as a library and simply call it from within my java program to query the mic input levels, something like:
ExportedMaxObject o = new ExportedMaxObject();
int currentMicLevel = o.getMicInputLevel();
I know Max/Msp makes somehow possible to use mxj and mxj~ Java objects to access data to and from Max but it is not clear to me how they are used. Do I need to have Max running or installed in my computer in order to use it? I am looking for a solution where I can embed this into my Java program and use my own UI so I can distribute it without any dependencies to Max/Msp.
Thanks in advance for any help.
I would say it is very unlikely that you will find a way to load a Max patch in Java.
The other way around might be possible: building an app in Max that incorporates your Java work, however it's important to note that Java support in Max has not received a lot of attention since Apple dropped it.
I have a system in Java where different classes stores different information. There is a main class where the user will input the information of these classes and make them interact with each other. After the user is done, he will exit the system. The Next time the user recompile the project, all the previously entered data should be there. The user can use that same data or add more information.
So Simply, How to pause/save the system on close and resume it when I execute it again?
PS. I can't use Database in this. It must be something else.
The Next time the user recompile the project
Users dont recompile projects. They just run your app.
You keep saying 'not DB', so, then, the answer is trivially: impossible.
A database, by definition, persists some data, hence the name: It's a base of data. If that's off the table, you're out of luck and what you want is not possible.
Perhaps you are careless in your wording there as well and all you mean is perhaps:
I do not want to bother with forcing the user to install a database
Okay, then, don't. Use an in-process database, such as h2. The user doesn't know an SQL-based database system is involved, all they see is a file appear. No extra processes are launched.
I hate SQL
Okay, then, don't. There are tools out there that turn an entire object structure into a bag o bytes which you can then save to a file, for example Jackson which can turn one object (which can contain all the relevant user data if you want) into JSON data, which you can then save to a file, and restore later. Of course, if someone trips over a powercable halfway through writing it, the file is corrupted. There are ways to fix that (save to .tmp, then move it into place, as that's usually atomic), but you're sort of committed to re-inventing the wheel here, due to your insistence you don't want databases.
I just want to save the entire system state
You can't. Not how java works.
Can't I do it with zero dependencies?
There's java's built in serialization system, which sucks, has a list of caveats as long as my leg on how to use it, and is more or less disliked by the maintainers of the java platform itself. This is not the way to go. It also still won't 'save the entire system state', it just saves one object, and does a much worse job at this than e.g. jackson.
I want to count the number of times my servlet program ran in the application.
I have used count variable in doPost method of the servlet. Every time I run the application the count will increase. But if close the editor (eclipse/netbeans) or if I close the project then my count will be reset to zero.because my servlet container will also stop running.
One way is to save my count value in data base. Is there any other way apart from database where I can store my count value even after closing the editor or project. Can I store my count value in file?
In general you can store the count value whenever you want, file, database of any kind and so on.
However I would like to mention that servlets in general are designed to run in environments where many users can access the servlet simultaneously, so you always have to think about the synchronization.
Multiple instances of the serlvet class can exist (it's up to you web container when to create them).
In general what you need to do is:
Get the current value
Increase the value by 1
Store the value
Now this thing has to be carefully implemented.
Depending on the underlying layer you might want to consider using Transactions:For example for postgesql
Another example is using a NoSQL store like Redis:
Read Pattern counter section
The best way to implement a distributed counter really depends on the underlying storage so its hard to advice on anything more specific, the choice is yours.
Another aspect I would like to mention is a distributed nature of web application.
If its a real world application, eventually you might want to run it on multiple service and 'scale it out' (at least its a trend in a modern server side software in my opinion).
From this perspective, the application can't really use local file and, say, windows registry because the file system is not a distributes thing (unless you use a distributed file system).
Hope, this helps
I am completing an Android program to classify the genre of a song on a mobile phone. The classification code is implemented in a native shared library (not written by me). Unfortunately, it only classifies correctly the first time, any times after then it just returns the previous result. I was thinking this was to do with the fact that some of the variables in the code are declared static and that they are not being instantiated properly between classifications.
I need to know how I can free the memory associated with a native library after each classification so that all the variables are instantiated correctly in the next classification.
I currently call the native library via a class, can I 'free' the class (like in C/C++) explicitly without waiting for the garbage collector?
#qrtt1 was right, searched through the code for a reset function and I found one... thanks!
I have a Java program which has a number options it allows the user to change, mostly via JComboBox. The only problem is that every time the user closes the program the settings reset, because they are not actually stored anywhere. Is there a standard way to give Java programs persistent state between runs? I could write the settings to a temporary file, but it seems like there should be a more elegant solution.
The Java Preference API is the way to go.
You can find an overview here.
If you keep your entire application Javabean-safe, and extend the standard JFrame classes such and such, you should be able to serialise the entire object graph to file and reload it.
See Restore previously serialized JFrame-object, how? for more details.