top - 21:06:05 up 118 days, 19:07, 1 user, load average: 0.01, 0.02, 0.00 Tasks: 123 total, 1 running, 122 sleeping, 0 stopped, 0 zombie Cpu(s): 0.5%us, 0.2%sy, 0.0%ni, 99.3%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 1020224k total, 947560k used, 72664k free, 5692k buffers Swap: 2097144k total, 500732k used, 1596412k free, 65308k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ SWAP COMMAND 29457 wildfly 20 0 3183m 438m 6152 S 0.3 44.0 41:27.36 355m java 19826 james 20 0 2725m 281m 6788 S 1.0 28.3 28:28.08 39m java ...
Derby database backup script
TweetPosted on Friday Feb 20, 2015 at 02:49PM in Technology
As Roller, and a virtual machine on VMware Fusion, I wrote an another Ant script which backups an Apache Derby database to automate backup of data of my Apache James server. the script has easy purge function as a target named purge
too. intended environment is as follows:
-
Linux server
-
Accepts connection via ssh
-
Has executable
ij
command which is simple CLI JDBC frontend program shipped with Derby -
Requires Derby instance to listen a port
It works as follows:
-
Invoke
SYSCS_UTIL.SYSCS_FREEZE_DATABASE()
withij
to freeze the database -
Create a tarball of the database
-
Invoke
SYSCS_UTIL.SYSCS_UNFREEZE_DATABASE()
withij
to unfreeze the database -
Download the tarball
-
Delete the tarball
A bad thing is that due to lack of streaming download in sshexec task, it needs extra free space on the server. the script can be obtained from my GitHub repository.
Memory usage tuning of Java8 on Linux
TweetPosted on Thursday Feb 19, 2015 at 06:20PM in Technology
I have a WildFly and a Apache James server instance on Linux based VPS which have 1GB RAM. these days they allocates large swap area while they only used fewer than 1GB for actual memory consumption. performance is not bad for just serving without any administrative operations, but some operation tend to slow due to large swap size so I configured some to reduce swap size.
Environment
-
CentOS 6.5
-
Oracle JDK8u31
Before
Configure JVM params
Set following JVM params to James:
-XX:ReservedCodeCacheSize=100m -XX:MaxMetaspaceSize=128m -XX:CompressedClassSpaceSize=128m
Set following to wildfly as well:
-XX:ReservedCodeCacheSize=100m -XX:MaxMetaspaceSize=256m -XX:CompressedClassSpaceSize=128m
After
These parameters reduced swap size as follows:
top - 19:33:09 up 119 days, 17:34, 3 users, load average: 0.41, 0.24, 0.10 Tasks: 130 total, 1 running, 129 sleeping, 0 stopped, 0 zombie Cpu(s): 0.3%us, 0.3%sy, 0.0%ni, 99.3%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 1020224k total, 924232k used, 95992k free, 8400k buffers Swap: 2097144k total, 407972k used, 1689172k free, 90320k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ SWAP COMMAND 30181 wildfly 20 0 2141m 405m 6308 S 0.0 40.7 5:11.08 272m java 29949 james 20 0 1677m 249m 6720 S 0.7 25.0 5:52.41 46m java
UPDATE: Now swap size grown as almost before adding configuration so sadly now I’m not sure whether it works or not.
Setting swappiness
vm.swappiness
defines how often the swap file is used. setting lower value means lower swap size but it doesn’t simply mean better performance. you can adjust it via executing following command to apply instantly:
sudo sysctl vm.swappiness=1
Add following definition to /etc/sysctl.conf
to set it as persistent one:
vm.swappiness = 1
Following discussions may be useful:
Importing entries into Roller
TweetPosted on Thursday Feb 19, 2015 at 11:54AM in Technology
Recently I migrated old articles that wrote within hand made blog (?) engine to Roller. there’s a useful Groovy script that placed in source distribution of Roller. I referenced that script and created a standalone Java program which imports entries that saved as RSS 2.0 XML. the program is intended to use with WordPress WXR file but I’ve never used it with real WXR file which WordPress produced because I don’t have any WordPress instances for my own. I used it for a XML file which produced by Rome and before import I added some tweak by hand.
I think it’s harder to use without any modification but it may be helpful for someone wants to import entries with API for Roller, so I pushed the program into GitHub.
Tags: roller
What are lookahead / lookbehind of regex?
TweetPosted on Sunday Feb 15, 2015 at 10:04AM in Technology
I’m learning regex with Mastering Regular Expressions, 3rd Edition. it’s interesting because long time I didn’t understand lookahead / lookbehind correctly. so I leave some examples for better understanding. tests were ran against jEdit 5.2.0 on Oracle Java 1.8.0_31.
Given string
(1) http://blog1.example.com/roller/ (2) http://blog2.example.com/mt/ (3) http://blog3.example.com/wordpress/
Positive lookahead
Positive lookahead ensures that the matching has following fragment which matches to given regex inside parenthesis. example\.com(?=/roller/)
matches against only (1)
.
Negative lookahead
Negative lookahead simply reverses that condition. example\.com(?!/roller/)
matches against (2) and (3)
.
Positive lookbehind
Positive lookbehind means that the matching has the preceding fragment which matches to given regex inside parenthesis. (?<=blog1\.)example\.com
matches against only (1)
.
Negative lookbehind
Negative lookbehind simply reverses that condition. (?<!blog1\.)example\.com
matches against (2) and (3)
.
Tags: regex
Doesn't InitialContext.doLookup() bring resource leak?
TweetPosted on Thursday Feb 12, 2015 at 12:00PM in Technology
There’s a convenient static method called InitialContext.doLookup() which introduced at Java6. it uses Generics so we don’t need to cast returned object by hand. implementation is very simple as follows:
public static <T> T doLookup(String name) throws NamingException { return (T) (new InitialContext()).lookup(name); }
I think it’s bad because there’s no way to invoke InitialContext#close() because doLookup()
doesn’t save the reference to InitialContext
instance anywhere. if it acquired some resource such as TCP socket it may brings resource leak.
Personally I will avoid use of doLookup()
because of this reason while there are many opinion that it doesn’t need to close explicitly at most of environments. I guess I’m too sensitive about it but I have a history of strugging with leak of JDBC resources (Connection
, Statement
and ResultSet
) so I’m very curious about this kind of problem.
Sometimes handling of InitialContext
is annoying because it should be keep opened during use of returned instance on some environments or implementations. also I wonder why InitialContext
doesn’t implemented AutoCloseable
while it’s a perfect candidate for it.
For Servlets you could write as follows:
public class TestServlet extends HttpServlet { private Context context; private DataSource dataSource; @Override public void init() throws ServletException { try { context = new InitialContext(); dataSource = (DataSource) context.lookup("java:comp/env/jdbc/derby"); } catch (NamingException e) { throw new ServletException(e); } } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try (Connection cn = dataSource.getConnection()) { // database operations } catch (SQLException e) { throw new ServletException(e); } } @Override public void destroy() { if (context != null) { try { context.close(); } catch (NamingException e) { // no-op } } } }
In jberetweb, I wrote an interface named JobOperatorWork and a method intended to use with it called doWorkWithJobOperator() for ease of handling of InitialContext
closing.
In Java EE environment, you could simply use @Resource
or @EJB
for avoiding annoying procedure of closing.
Tags: javaee