patternpythonMinor
Text parsing and calculator (calculate Java heap memory)
Viewed 0 times
textjavaheapparsingcalculatememoryandcalculator
Problem
I'm writing small tool, which must execute
I added some
```
# JVM with jmap statistic
def java_heap_save(log):
subprocess.call('jmap -heap %s > %s' % (java_pid, log), shell=True, stdout=devnull, stderr=devnull)
with open(log, 'r') as logread:
data = logread.readlines()
# this is just to display 'what will parsing'
print(data)
# here is two list, first - index of item in data, second - for message on terminal
nums = [40, 35, 30, 25, 20]
messages = ['PS Perm Generation', 'PS Old Generation', 'Yong To Space', 'Yong From Space', 'Yong Eden Space']
# I don't found how to create dynamic variable names, so use dictionary
count_names = {}
count_numbers = 1
for num, mess in zip(nums, messages):
# print(mess + ' used '+ str((int(data[num].split(' ')[-2]) / 1024)) + ' KB;')
# from log we got data in bytes, convert here to megabatyes
res = (int(data[num].split(' ')[-2]) / 1024)
print('%s used %s KB;' % (mess, res))
# save value
count_names[count_numbers] = int(res)
count_numbers += 1
print('\nJVM Heap memory stats:\n')
mem_total = (int(data[7].split(' ')[-2]) / 1024 / 1024)
print('MemTotal = %s MB;' % mem_total)
# next few lines - my first attempt, then was rewrited to loop above
# ps_perm_gen = (int(data[40].split(' ')[-2]) / 1024)
# print('PS Perm Generation used = %s KB;' % ps_perm_gen)
# ps_old_gen = (int(data[35].split(' ')[-2]) / 1024)
# print('PS Old Generation used = %s KB;' % ps_old_gen)
# young_to_space = (int(data[30].split(' ')[-2]) / 1024)
# print('Yong To Space used = %s KB;' % young_to_space)
# young_from_space = (int(data[25].split(' ')[-2]) / 1024)
# print('Yong From Space used = %s KB;' % young_from_space)
# young_eden_space = (int(data[20].split(' ')[-2]) / 1024)
# print('Yong Eden Space used = %s KB;' % youn
jmap -heap, write output to log, then takes some data from log and calculate used memory.I added some
debug to it.```
# JVM with jmap statistic
def java_heap_save(log):
subprocess.call('jmap -heap %s > %s' % (java_pid, log), shell=True, stdout=devnull, stderr=devnull)
with open(log, 'r') as logread:
data = logread.readlines()
# this is just to display 'what will parsing'
print(data)
# here is two list, first - index of item in data, second - for message on terminal
nums = [40, 35, 30, 25, 20]
messages = ['PS Perm Generation', 'PS Old Generation', 'Yong To Space', 'Yong From Space', 'Yong Eden Space']
# I don't found how to create dynamic variable names, so use dictionary
count_names = {}
count_numbers = 1
for num, mess in zip(nums, messages):
# print(mess + ' used '+ str((int(data[num].split(' ')[-2]) / 1024)) + ' KB;')
# from log we got data in bytes, convert here to megabatyes
res = (int(data[num].split(' ')[-2]) / 1024)
print('%s used %s KB;' % (mess, res))
# save value
count_names[count_numbers] = int(res)
count_numbers += 1
print('\nJVM Heap memory stats:\n')
mem_total = (int(data[7].split(' ')[-2]) / 1024 / 1024)
print('MemTotal = %s MB;' % mem_total)
# next few lines - my first attempt, then was rewrited to loop above
# ps_perm_gen = (int(data[40].split(' ')[-2]) / 1024)
# print('PS Perm Generation used = %s KB;' % ps_perm_gen)
# ps_old_gen = (int(data[35].split(' ')[-2]) / 1024)
# print('PS Old Generation used = %s KB;' % ps_old_gen)
# young_to_space = (int(data[30].split(' ')[-2]) / 1024)
# print('Yong To Space used = %s KB;' % young_to_space)
# young_from_space = (int(data[25].split(' ')[-2]) / 1024)
# print('Yong From Space used = %s KB;' % young_from_space)
# young_eden_space = (int(data[20].split(' ')[-2]) / 1024)
# print('Yong Eden Space used = %s KB;' % youn
Solution
First off, using
Secondly, all the commented out lines of code, like
You also have a lot of not-so-great variable names. A few examples include
Finally, improve your comments. Right now, not very many of your comments are very good. A few examples include:
Comments should describe hard-to-understand parts of code in detail, but only if needed. If the code is clear enough to understand, then don't use a comment.
% for string formatting is deprecated if you're using Python 2.6 or higher. You should be using str.format instead. Here's an example of how you can use str.format.# str.format without positional or named parameters
print "{} {}".format("Hello", "world")
# str.format with positional parameters
print "{1} {0}".format("world", "Hello")
# str.format with named paramters
print "{word1} {word2}".format(word1="Hello", word2="world")Secondly, all the commented out lines of code, like
# ps_perm_gen = (int(data[40].split(' ')[-2]) / 1024) should be entirely removed. Stuff like this is an eyesore to look at.You also have a lot of not-so-great variable names. A few examples include
nums, res, data, and count_numbers, Variable names should describe the purpose of the variable as best as possible.Finally, improve your comments. Right now, not very many of your comments are very good. A few examples include:
# next few lines - my first attempt, then was rewrited to loop above
# this is just to display 'what will parsing'
# save valueComments should describe hard-to-understand parts of code in detail, but only if needed. If the code is clear enough to understand, then don't use a comment.
Code Snippets
# str.format without positional or named parameters
print "{} {}".format("Hello", "world")
# str.format with positional parameters
print "{1} {0}".format("world", "Hello")
# str.format with named paramters
print "{word1} {word2}".format(word1="Hello", word2="world")# next few lines - my first attempt, then was rewrited to loop above
# this is just to display 'what will parsing'
# save valueContext
StackExchange Code Review Q#64271, answer score: 4
Revisions (0)
No revisions yet.